17

The C clock() function just returns me a zero. I tried using different types, with no improvement... Is this a good way to measure time with good precision?

#include <time.h>
#include <stdio.h>

int main()
{
    clock_t start, end;
    double cpu_time_used;

    char s[32];

    start = clock();

    printf("\nSleeping 3 seconds...\n\n");
    sleep(3);

    end = clock();

    cpu_time_used = ((double)(end - start)) / ((double)CLOCKS_PER_SEC);

    printf("start = %.20f\nend   = %.20f\n", start, end);
    printf("delta = %.20f\n", ((double) (end - start)));
    printf("cpu_time_used  = %.15f\n", cpu_time_used);
    printf("CLOCKS_PER_SEC = %i\n\n", CLOCKS_PER_SEC);

    return 0;
}
Sleeping 3 seconds...

start = 0.00000000000000000000
end   = 0.00000000000000000000
delta = 0.00000000000000000000
cpu_time_used  = 0.000000000000000
CLOCKS_PER_SEC = 1000000

Platform: Intel 32 bit, RedHat Linux, gcc 3.4.6

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pietro
  • 12,086
  • 26
  • 100
  • 193

6 Answers6

26

clock() reports CPU time used. sleep() doesn't use any CPU time. So your result is probably exactly correct, just not what you want.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • You are right. With a for loop with some math inside it returns a reasonable value. Thank you! – Pietro Jan 25 '10 at 18:27
6

man clock. It's not returning what you think it is. Also man gettimeofday - it's more likely what you want.

Aidan Cully
  • 5,457
  • 24
  • 27
4

clock_t is an integer type. You can't print it out with %f. See Fred's answer for why the difference is 0.

Community
  • 1
  • 1
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • `clock_t` needs to be an arithmetic type, which could be an integer or a floating type. So you might be able to print it out with `%f`, but to be safe(er), the values should be cast to `(double)` in the `printf()` call so it'll work right regardless of whether it's an integer or floating type. – Michael Burr Jan 25 '10 at 20:12
  • @Michael, have you ever seen an implementation where it was a floating type? I would imagine that struct tms among other things would be pretty messed up if it was. – Paul Tomblin Jan 25 '10 at 20:16
  • I couldn't point you to a specific instance (but then again, unless I look, I don't know if Windows or whatever is using an integer type either). I do know that several platforms use a floating type for `time_t` - I dunno if this means they might be more likely to do similar for `clock_t` or not. The type of `clock_t` should have no impact on `struct tm` at all. – Michael Burr Jan 25 '10 at 20:50
  • It's hard to imagine a case where the "number of ticks between two operations" would be non-integer. – Paul Tomblin Jan 25 '10 at 21:03
  • 1
    I'm not saying it's common or makes sense (I actually don't know if it's ever used), but people who know more about the possible requirements than I do (the standards committee members) specifically permitted `clock_t` to be a floating type. FWIW, I also checked in Plauger's Standard C Library book and Harbison & Steele, and they each mention that ANSI/ISO C's `clock_t` can be float. – Michael Burr Jan 25 '10 at 22:08
  • 2
    The famous example is the Patriot Missile system. Its clock used floating point. As a result, its absolute precision decreased when the system ran for longer periods of time, such as in the Gulf War. This caused a subsequent loss in guidance accuracy, interception failures, and multiple deaths. So yes, Paul Tomblin is more than right when he says that "things would be pretty messed up" – MSalters Jan 26 '10 at 11:37
4
 printf("start = %.20f\nend   = %.20f\n", start, end);

should be:

 printf("start = %d\nend   = %d\n", start, end);
4

Calling sleep() isn't going to use up any CPU time. You should see a little difference, though. I corrected your printf type mismatch bug in this line:

printf("start = %.20f\nend   = %.20f\n", start, end);

And it gave reasonable results on my machine:

start = 1419
end   = 1485
delta = 66
cpu_time_used  = 0.000066000000000
CLOCKS_PER_SEC = 1000000

You might try gettimeofday() to get the real time spent running your program.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
2

You probably need

double get_wall_time(){ struct timeval time; if (gettimeofday(&time,NULL)){ return 0; } return (double)time.tv_sec + (double)time.tv_usec * .000001; } and usage something like

double wall0 = get_wall_time(); double cpu0 = get_cpu_time(); for(long int i = 0; i<=10000000;i++){ func1(); } double wall1 = get_wall_time(); double cpu1 = get_cpu_time(); cout << "Wall Time = " << wall1 - wall0 << endl; cout << "CPU Time = " << cpu1 - cpu0 << endl;

instead of clock()

as clock only and only counts time spent in CPU only based on performance counters. but you can get result by using above function. just to verify your application run it with time command

like time ./a.out

output of time command :

real 0m5.987s user 0m0.674s sys 0m0.134s

and output of custom function Wall Time = 5.98505 CPU Time = 0.8

Sumit Lubal
  • 329
  • 2
  • 13