I'm trying to measure some activity in C (Matrix multiplying) and noticed that I should do something like this:
clock_t start = clock();
sleep(3);
clock_t end = clock();
double elapsed_time = (end - start)/(double)CLOCKS_PER_SEC;
printf("Elapsed time: %.2f.\n", elapsed_time);
The output is:
Elapsed time: 0.00.
Why is this happening?
You must use
time_t start = time(NULL);
andtime_t end = time(NULL);
to get the correct values.clock
estimates the CPU time used by your program; that's the time the CPU has been busy executing instructions belonging to your program.sleep
doesn't perform any work, so it takes no noticeable CPU time (even if it takes wallclock time).If you want to measure wallclock time, use
time
:will print a number close to three.
As a side note, if you want to measure execution time in a more precise manner (milliseconds),
time
is not precise enough. You can usegettimeofday
instead:Use QueryPerformanceFrequency() as described in Orwells answer or use the GetSystemTimeAsFileTime() function. The latter has 100 ns granularity but does not increment at that rate. Its increment depends on underlaying hardware and the setting of multimedia timer resolution. Keep in mind that the frequency returned by
QueryPerformanceFrequency()
is treated as a constant. However, since it is generated by hardware it has an offset and a drift in time too. Measuring periods in time by usingQueryPerformanceCounter()
will typically be accompanied by errors of many microseconds per second. I've given this and this answer about similar matters.If you don't care about being tied to Windows, you can try the high resolution timer. It's is a lot more precise than time(), which only has a precision of a single second because it the uses UNIX format.