#1
  1. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13

    different lengths of microseconds


    does anyone know why this would happen?

    this code:
    Code:
    #include <stdio.h>
    #include <sys/time.h>
    
    main()
    {
    	int i;
    
    	struct timeval tv;
    	for(i = 0; i < 1000; i++) {
    		gettimeofday(&tv, NULL);
    		printf("%d %d\n", tv.tv_sec, tv.tv_usec);
    	}
    }
    results in:
    Code:
    1059440472 991520
    1059440472 992627
    1059440472 993719
    1059440472 994760
    1059440473 5314
    1059440473 5490
    1059440473 5508
    ...
    ...
    ...
    1059440473 6211
    1059440473 6223
    1059440473 6236
    1059440473 226237
    1059440473 226294
    1059440473 226307
    why the different lengths of microseconds?
  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Multi-tasking, interrupt requests, etc.

    Really, unless you're using some real-time OS (what's that called again? QNX?), there's no guarantee that -any- two consecutive machine code instructions (let alone C statements, or even worse, interpreted statements like PHP/bash) are executed one after the other without being interrupted by the OS (for whatever reason).
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    that's fine. so other stuff could be slotted between one command and another in your code, from elsewhere basically, unbeknownst by you or your code. no problem.

    ah sorry, i now realise i've worded the question really badly :). "why the different lengths of microseconds?" should be "why the varying number of digits / accuracy in different readings of microseconds?"

    just seemed odd that the microseconds would be 6 digits long for a good stretch, then switch to 4 digits for a stretch, then back to 6 - for no apparent reason. i know that must seem a pretty inconsequential question, and also you can't rely on the accuracy of those timings above, what 3 digits?, but i am interested if anyone's got any thoughts on an explenation why it'd go from 6 to 4 then back to 6 like that?

    thanks.
  6. #4
  7. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    1059440473 6236
    1059440473 226237
    I think you'll agree with me that that jump is the result of task switching or interrupt handling.

    Further back, there's

    1059440472 994760
    1059440473 5314
    Now let's think here, for a minute. How many microseconds are there in a second, anyway? About a million, right? :) The counter probably just wrapped around during a task-switch or whatever.

    So basically, it's showing a varying amount of digits because you didn't tell printf otherwise ("%07d" vs. "%d").
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally posted by balance
    that's fine. so other stuff could be slotted between one command and another in your code, from elsewhere basically, unbeknownst by you or your code. no problem.

    ah sorry, i now realise i've worded the question really badly :). "why the different lengths of microseconds?" should be "why the varying number of digits / accuracy in different readings of microseconds?"

    just seemed odd that the microseconds would be 6 digits long for a good stretch, then switch to 4 digits for a stretch, then back to 6 - for no apparent reason. i know that must seem a pretty inconsequential question, and also you can't rely on the accuracy of those timings above, what 3 digits?, but i am interested if anyone's got any thoughts on an explenation why it'd go from 6 to 4 then back to 6 like that?

    thanks.
    Uh, because you rolled over into the next second?

    [...]
    struct timeval tv;
    for(i = 0; i < 1000; i++) {
    gettimeofday(&tv, NULL);
    printf("%d %d\n", tv.tv_sec, tv.tv_usec);
    }
    }--------------------------------------------------------------------------------

    results in:

    code:--------------------------------------------------------------------------------
    [...]
    1059440472 993719
    1059440472 994760
    1059440473 5314
    1059440473 5490
    [...]
    When you tick over into the next second, the microseconds count naturally goes from 999,999 to 0 and then starts counting up to 999,999 again.

    EDIT:
    Sorry, I should not have been so blunt. I could point out that I hadn't had my morning coffee yet, but that would be a cop-out.
    Last edited by dwise1_aol; July 29th, 2003 at 04:12 PM.
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    13
    dear oh dear. i wish you could delete whole threads. ok, thanks very much.

IMN logo majestic logo threadwatch logo seochat tools logo