Saturday, May 22, 2010

I have a problem with the clock() function in the C programming language. Details below?

i'm using the clock() function to find out how many clock ticks have passed from one point in the program to another. I'm using Turbo C to compile but even gcc has the same problem.





Here's the code





double difference;


clock_t time1;


clock_t time2;


int i;





time1=clock();


for(i=0;i%26lt;5;i++){


printf("Time starts");


}


time2=clock();


difference=time2-time1;


printf("\n%f",difference);





When i do a trace per line, time1, time2, and difference have values. But when i run the program normally, time1, time2 and difference all have a value of zero. HOw do i fix this. I really need help. This is due in four days and i still have lots of things to finish for this program. Thank you.

I have a problem with the clock() function in the C programming language. Details below?
clock is in seconds and most computers can execute that program in less than a second so the computer is right, it does not matter which compiler. When you trace through it, the second clock function is called later and so there is a difference.





You'll need to use a clock with at least microsecond precision, you could do this with the microtime function in POSIX but i don't know of any platform way to do it(or a windows way to do it for that matter).





email me at iammisc@gmail.com if you need more help.
Reply:To see if it really works,check by putting a sleep() between these 2 and check if the difference is the amount you've asked it to sleep....





Also trying printing to stderr...Because printing to stdout might be buffered and printed...Printing to stderr(which i hope is your terminal) might cause some delay...

pink

No comments:

Post a Comment