clock() function

Hey all,

i need a program to get the CPU ticks at certain points of my program. So, i thought about using the clock function, but i'm having a hard time figuring out how it really works. I wrote this simple program to try to understand it but it made me feel more confused:

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

clock_t start, end;
double cpu_time_used;

int main(int argc, char* argv[])
{
clock_t start, end;
double cpu_time_used;
long int i;

for (i=1; i<ITERATIONS; i++)
;
start = clock();

for (i=1; i<1000000000; i++)
;
end = clock();

printf("start: %d\n",start);
printf("end: %d\n",end);

cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("cpu_time_used: %f\n",cpu_time_used);

return 0;
}

when i run my program with ITERATIONS = 100000, i get as result
start: 0
end: 2760000
cpu_time_used: 2.760000

it also happens when ITERATIONS = 1.

Why is that?!?

thanks in advance,
Breno Kastrup

A few clues like what computer, what os, and what compiler would help :frowning: But in general...
for(i=1; i<1000000; i++);
is really equivalent to
i=1000000;

and your compiler may be smart enough to remove the loop especially if the optimizer is turned on. Look at the options for your compiler to be sure that you aren't optimizing. If you are not optimizing, specify the option to use a symbolic debugger. Some compilers that would otherwise remove a loop will leave it in under that circumstance. If you know your systems assembler, use the the option to produce assembly code to see what is happening. You also may have a #pragma available to influence stuff like this...check you compiler's docs for that.

Yes, your compiler is most likely optimizing the code. If you work with gcc, this is the second level of optimization, represented by

gcc -O2.

option (and this is default). You might want to compile your program with

gcc -O0

which removed all optimizations, or might want to #include math.h and include some math operation in the loop. For example,

for (i=1; i<1000000000; i++)
sqrt(i);
;

This gives the compiler something to do, and it would not be able to optimize it.

Hope that helps.

Kapil Sharma

A very important point - the C standard does not say anyhting about the granularity of clock() - a compiler can have it check time once a second and increment the
variable by CLOCKS_PER_SEC.

This means it is possible depending on your implmentation, that you can get zero, CLOCKS_PER_SEC, CLOCKS_PER_SEC * 2 and so on, never getting any intermediate value. Don't use clock() if you need high granularity.

use gettimeofday() --

I think that clock() is cpu time while gettimeofday() is wallclock time. Maybe getrusage() should be used... But I have never used clock().

gettimeofday() returns a timeval struct. You can get a start value and an end value.

I think I posted on the forums how to do a delta of the times in timeval structs.
getrusage() returns CPU time, which is more useful than elapsed time on a multiprocessing box.

In the event you want cpu information, just using

time <filename>

at the commandline gives you the same information without having to instrument your code for getrusage().