Fun with gettimeofday [Archive] - C Board

PDA

View Full Version : Fun with gettimeofday


Yasir_Malik
10-03-2003, 03:42 PM
The tv_usec field in struct timeval is supposed to store the number of microseconds since January 1, 1970 00:00. But when I do the following:
#include <sys/time.h>

int main()
{
int i;
struct timeval start, end;
struct timezone tzp;

gettimeofday(&start, &tzp);

for(i = 0; i < 100000; i++){}

gettimeofday(&end, &tzp);

printf("start: %lu\n", start.tv_usec);
printf("end: %lu\n", end.tv_usec);
printf("time: %lu\n", end.tv_usec - start.tv_usec);

return 0;
}

The values I get for start.tv_usec and end.tv_usec are not increasing after every run of the program. For example, I'll get
start: 609659
end: 610257

and in another run afterwards I'll get
start: 204615
end: 205695

Salem
10-03-2003, 04:13 PM
> The tv_usec field in struct timeval is supposed to store the number of microseconds since January 1, 1970 00:00
No, tv_sec stores this value

tv_usec is 0 to 999999

If tv_usec is 999999, adding 1 resets it back to 0, and increments tv_sec

Yasir_Malik
10-03-2003, 04:27 PM
Thanks. You're a pro.

newbie_grg
10-05-2003, 11:58 AM
Believe me dude Salem is the only REAL programmer we have in this forum... :)

Lynux-Penguin
10-05-2003, 02:22 PM
I don't know I think Prelude, and Hammer classify as programmers too.

-LC

KneeLess
10-05-2003, 09:56 PM
Originally posted by Lynux-Penguin
I don't know I think Prelude, and Hammer classify as programmers too.

-LC

But aren't we all? :p

twm
10-06-2003, 07:38 AM
>But aren't we all? :p
I'm not. And proud of it too. :D Programmers are always running around mumbling about deadlines and looking worried. I run around making deadlines and looking goofy. That's the way to live life! :cool:

Jaguar
10-06-2003, 10:47 AM
I agree with you all.
But where is vVv and where are his posts? :(