Reputation: 253
I finished the programming of a class project, but I still need to get the running time. I tried clock() function, but it didn't work.
int main()
{
clock_t start_time=clock();
(my codes)
clock_t end_time=clock();
printf("The running time is %f", end_time-start_time);
}
I guess this is the right way to use clock function, am I right? But I got nothing but 0.000000. Could someone tell me the right way? Thank you!
Upvotes: 2
Views: 294
Reputation: 70971
clock_t
is not a float
, but an integer type, most probably a long
.
So you might use %ld
to printf()
it.
Also clock()
does not return seconds, but CPU ticks. So to get seconds the value returned by clock()
shall be devided by the system constant CLOCKS_PER_SEC
.
Upvotes: 2
Reputation: 8325
phil you did in the right way (maybe you should also divide by CLOCKS_PER_SEC)
float time = (float)(end_time-start_time)/(float)CLOCKS_PER_SEC;
printf("The running time is %f", time);
and don't forget to cast as "float" because if you have as result 0.5 it will be rounded down to 0.0 because of integer division.
After that how much time takes you program to execute? because as far as I know, clock has a capability of measuring time of few milliseconds, so if you program takes less time than few milliseconds, it is likely to not detect this time (clock is very good for measuring times of several seconds with relatively good precision)
Upvotes: 0