Why C clock () returns 0

I have something like this:

clock_t start, end; start=clock(); something_else(); end=clock(); printf("\nClock cycles are: %d - %d\n",start,end); 

and I always get as output "Clock cycles: 0 - 0"

Any idea why this is happening?

(To give a little detail, the something_else () function does an exposition from left to right using the montgomery view, moreover, I donโ€™t know for sure that the something_else () function really takes some less time.)

This is on Linux. The result of uname -a is:

Linux snowy.*****.ac.uk 2.6.32-71.el6.x86_64 #1 SMP Fri May 20 03:51:51 BST 2011 x86_64 x86_64 x86_64 GNU/Linux

+8
c linux clock
source share
6 answers

clock function does not measure processor clock cycles.

C says clock "returns the best approximation of implementations to the processor is the time used by the program since the beginning of the corresponding era associated with the implementation only to call the program."

If between two consecutive clock calls your program takes less time than one unity of the clock function, you can get 0 .

The POSIX clock defines unity with CLOCKS_PER_SEC as 1,000,000 (unity is 1 microsecond).

http://pubs.opengroup.org/onlinepubs/009604499/functions/clock.html

To measure the clock cycles in x86 / x64, you can use the built-in assembly to restore the clock counter of the rdtsc processor time counter.

+7
source share

I assume the reason is that something_else() consumes so little time that it exceeds clock() accuracy . I tried calling clock() twice and start and end are zero, but the result is reasonable when I do a lot of time between them.

Here is my test code snippet:

 int main(void) { clock_t start, end; start = clock(); int c; for (int i = 0; i < 100; i++) { for (int j = 0; j < (1<<30); j++) { c++; } } end = clock(); printf("start = %d, end = %d\n", start, end); return 0; } 

And the result on my computer:

 start = 0, end = 27700000 

Also two tips ๏ผš

  1. When testing, do not use compiler optimization . You might think that something_else() is time consuming, but the compiler can simply ignore these operations (especially loops), since it considers them meaningless.
  2. Use sizeof(clock_t) on your platform to see the size of clock_t .
+6
source share

Well, do you want something_else() time to take? Try this:

 #include <sys/time.h> #include <stdio.h> #include <unistd.h> int main(void) { struct timeval start, end; long mtime, secs, usecs; gettimeofday(&start, NULL); something_else(); gettimeofday(&end, NULL); secs = end.tv_sec - start.tv_sec; usecs = end.tv_usec - start.tv_usec; mtime = ((secs) * 1000 + usecs/1000.0) + 0.5; printf("Elapsed time: %ld millisecs\n", mtime); return 0; } 
+5
source share

Check the value of CLOCKS_PER_SEC in time.h/clock.h . For example, on my system (Dev Cpp on Windows 7) it is just 1000 . As for my program, then 1000 ticks per second. Your something_else will be executed within microseconds. And therefore, clock() returns zero both before and after the function call.

On my system, when I replace your something_else with a time-consuming procedure like this

 for (unsigned i=0xFFFFFFFF;i--;); start=clock(); for (unsigned i=0xFFFFFFFF;i--;); end=clock(); 

I get

Timing Cycles: 10236 - 20593

In one of the linux boxes I find the following in bits/time.h

 /* ISO/IEC 9899:1990 7.12.1: <time.h> The macro `CLOCKS_PER_SEC' is the number per second of the value returned by the `clock' function. */ /* CAE XSH, Issue 4, Version 2: <time.h> The value of CLOCKS_PER_SEC is required to be 1 million on all XSI-conformant systems. */ # define CLOCKS_PER_SEC 1000000l 

So consider this before parsing the return value of clock()

+2
source share

The correct way to use clock () to measure time:

 printf("\nTime elapsed: %.2f\n",1.0*(end-start)/CLOCKS_PER_SEC); 

This is because clock_t is not guaranteed to be int or any other type.

+2
source share

I used the small program below to examine the wall clock and processor time.

On my test system this prints

CLOCKS_PER_SEC 1000000

CPU time usage resolutio n looks like 0.010000 seconds

gettimeofday changed to 9634 uS when CPU time changed to 0.010000

Gettimeofday resolution looks 1 us

 #include <stdio.h> #include <unistd.h> #include <sys/time.h> #include <ctime> int main(int argc, char** argv) { struct timeval now; // wall clock times struct timeval later; clock_t tNow = clock(); // clock measures CPU time of this Linux thread gettimeofday(&now, NULL); // wall clock time when CPU time first read clock_t tLater = tNow; while (tNow == tLater) tLater = clock(); // consume CPU time gettimeofday(&later, NULL); // wall clock time when CPU time has ticked printf("CLOCKS_PER_SEC %ld\n",CLOCKS_PER_SEC); double cpuRes = (double)(tLater - tNow)/CLOCKS_PER_SEC; printf("CPU time usage resolution looks to be %f seconds\n", cpuRes); unsigned long long nowUs = ((unsigned long long)now.tv_sec) * 1000000ULL; nowUs += (unsigned long long)now.tv_usec; unsigned long long laterUs = ((unsigned long long)later.tv_sec) * 1000000ULL; laterUs += (unsigned long long)later.tv_usec; printf("gettimeofday changed by %d uS when CPU time changed by %f seconds\n", (int)(laterUs - nowUs), cpuRes); // now measure resolution of gettimeofday gettimeofday(&now, NULL); later = now; while ((now.tv_sec == later.tv_sec) && (now.tv_usec == later.tv_usec)) gettimeofday(&later, NULL); nowUs = ((unsigned long long)now.tv_sec) * 1000000ULL; nowUs += (unsigned long long)now.tv_usec; laterUs = ((unsigned long long)later.tv_sec) * 1000000ULL; laterUs += (unsigned long long)later.tv_usec; printf("gettimeofday resolution looks to be %d us\n", (int)(laterUs - nowUs)); } 
+1
source share

All Articles