Easily measure elapsed time

前端 未结 26 2811
栀梦
栀梦 2020-11-22 04:13

I am trying to use time() to measure various points of my program.

What I don\'t understand is why the values in the before and after are the same? I understand thi

26条回答
  •  星月不相逢
    2020-11-22 04:49

    time(NULL) returns the number of seconds elapsed since 01/01/1970 at 00:00 (the Epoch). So the difference between the two values is the number of seconds your processing took.

    int t0 = time(NULL);
    doSomthing();
    doSomthingLong();
    int t1 = time(NULL);
    
    printf ("time = %d secs\n", t1 - t0);
    

    You can get finer results with getttimeofday(), which return the current time in seconds, as time() does and also in microseconds.

提交回复
热议问题