struct timeval first, second, lapsed;
struct timezone tzp;
gettimeofday (&first, &tzp);
sleep(1);
gettimeofday (&second, &tzp);
lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec;
printf("%d %d\n", lapsed.tv_sec, lapsed.tv_usec);
gettimeofday()
I'm trying to use gettimeofday in linux but I don't really understand how it works. I wrote a little piece of code to check it out, but I'm still confused. Here's the code:
I'm always getting 1, when printing the seconds passed but I'm not getting a constant number when printing the microseconds, why is that?
[Edited by - Deliverance on August 12, 2007 6:16:15 AM]
Here's my little benchmark template
static inline void dosomething() {}int main( int argc, char** args ) { struct timeval t1,t2; double time1, time2; gettimeofday(&t1, NULL); dosomething(); gettimeofday(&t2, NULL); time1 = t1.tv_usec; time1 = time1/1000000.0; time1 = time1 + t1.tv_sec; time2 = t2.tv_usec; time2 = time2/1000000.0; time2 = time2 + t2.tv_sec; cout << fixed << time2-time1 << "seconds" << endl; return 0;}
Quote: Original post by Deliverance
I'm always getting 1, when printing the seconds passed but I'm not getting a constant number when printing the microseconds, why is that?
Because depending on the load and granularity of the operating system scheduler, sleep(n) presents only a minimum elapsed time - in reality, it will vary, and offers no realtime guarantees. The time between sleep and wake up can be expected to be different each time that code is run.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement