PDA

View Full Version : GCC clock



sree88
January 7th, 2012, 05:35 AM
i have written a C code which has many function calls. my requirement is to get the time taken to execute a particular function in millisecs or microsecs i tried using the clock() function but the program doesnt complete execution.

lisati
January 7th, 2012, 05:41 AM
Thread moved to Programming Talk.

Welcome to the forums.

If you comment out the calls to clock(), does the program work as expected?

Bachstelze
January 7th, 2012, 05:43 AM
Post your code and describe the problem in more detail...

sree88
January 7th, 2012, 05:56 AM
i got the clock() function worked. there was some problem in the pgm. but canu explain how this code works

clock_t start,end;
double time;
start = clock();
//code to be executed
end = clock();
time= ((double) (end- start))/ CLOCK_PER_SEC;
printf("\n time:%f,(double)time);
}

i got this code from one of the ubuntu forums

Bachstelze
January 7th, 2012, 06:34 AM
The clock() function returns (an approximation of) the amount of CPU time used by your program since it started. By substracting the two values, you get the amount of CPU time used by your program between the two calls to clock(), and dividing by CLOCKS_PER_SEC gives the time in seconds.