Thread: Fun with gettimeofday

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Fun with gettimeofday

    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:
    Code:
    #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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Thanks

    Thanks. You're a pro.

  4. #4
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    haha..

    Believe me dude Salem is the only REAL programmer we have in this forum...
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I don't know I think Prelude, and Hammer classify as programmers too.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    42
    Originally posted by Lynux-Penguin
    I don't know I think Prelude, and Hammer classify as programmers too.

    -LC
    But aren't we all?
    Sigh, nothing ever works the first try.

    Register Linux User #314127

  7. #7
    root
    Join Date
    Sep 2003
    Posts
    232
    >But aren't we all?
    I'm not. And proud of it too. 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!
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  8. #8
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    I agree with you all.
    But where is vVv and where are his posts?
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a fun point-and-click mystery adventure game.
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-13-2008, 07:55 AM
  2. fun with desktop
    By CChakra in forum Windows Programming
    Replies: 5
    Last Post: 09-18-2008, 01:37 PM
  3. Auto Calling Derived Fun
    By Arlenik in forum C++ Programming
    Replies: 27
    Last Post: 06-02-2008, 11:17 AM
  4. Fun Learning a New Language
    By UnregdRegd in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-30-2003, 10:03 PM