Why are gettimeofday() intervals occasionally negative?
        Posted  
        
            by Andres Jaan Tack
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Andres Jaan Tack
        
        
        
        Published on 2010-05-06T10:26:15Z
        Indexed on 
            2010/05/06
            10:28 UTC
        
        
        Read the original article
        Hit count: 305
        
I have an experimental library whose performance I'm trying to measure. To do this, I've written the following:
struct timeval begin;
gettimeofday(&begin, NULL);
{
    // Experiment!
}
struct timeval end;
gettimeofday(&end, NULL);
// Print the time it took!
std::cout << "Time: " << 100000 * (end.tv_sec - begin.tv_sec) + (end.tv_usec - begin.tv_usec) << std::endl;
Occasionally, my results include negative timings, some of which are nonsensical. For instance:
Time: 226762
Time: 220222
Time: 210883
Time: -688976
What's going on?
© Stack Overflow or respective owner