By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,897 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

Problem with "time.h" library

P: 4
Hello everybody i want to execute a code which gives the duration of a running program by using the library "time.h" particularly the clock() function.
The problem is that i am not getting the right result and the program looks like:

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

int main(void)
{
float end;
clock_t start=clock();
.
.
.
.
end = (clock()-start)/CLOCKS_PER_SEC;
printf("%f",end);
return 0;
}

I have got 0.000000 as Output.
Thanks in advance for help.
Dec 10 '10 #1

✓ answered by donbock

All of the terms in the expression are integers so integer math is used, and then the integer result is cast to a float. Suppose your program takes less than a second to run -- then the result is zero.

Share this Question
Share on Google+
4 Replies


Expert 100+
P: 2,398
What do you expect the expression (clock()-start)%CLOCKS_PER_SEC to mean?
And why are you using modulo division?

You don't protect yourself from the possibility that this feature is not available. That is, you don't check if clock returned (clock_t)-1.
Dec 10 '10 #2

P: 4
It is supposed to be (clock()-start)/CLOCKS_PER_SEC instead of (clock()-start)%CLOCKS_PER_SEC, it has been a mistake and excuse me.
So what i want by using that expression is to get the elapsed time between the beginning of the program and the end in second.
Dec 10 '10 #3

Expert 100+
P: 2,398
All of the terms in the expression are integers so integer math is used, and then the integer result is cast to a float. Suppose your program takes less than a second to run -- then the result is zero.
Dec 10 '10 #4

P: 4
Thank you ,it works perfectly.
Dec 11 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.