Thread: computing time elapsed?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    4

    computing time elapsed?

    ok i looked thru the glibc manual and found the timeval struct... the code i've come up with is below:

    Code:
    /**
     * this function is for computing the time difference between timeval x and y
     * the result is stored in result
     */
    int
    timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
    {
        /* Perform the carry for the later subtraction by updating y. */
        if (x->tv_usec < y->tv_usec) {
            int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
            y->tv_usec -= 1000000 * nsec;
            y->tv_sec += nsec;
        }
        if (x->tv_usec - y->tv_usec > 1000000) {
            int nsec = (x->tv_usec - y->tv_usec) / 1000000;
            y->tv_usec += 1000000 * nsec;
            y->tv_sec -= nsec;
        }
    
        /* Compute the time remaining to wait.
        tv_usec is certainly positive. */
        result->tv_sec = x->tv_sec - y->tv_sec;
        result->tv_usec = x->tv_usec - y->tv_usec;
    
        /* Return 1 if result is negative. */
        return x->tv_sec < y->tv_sec;
    }
    
    int main(int argc, char *argv[])
    {
        struct timeval start, stop, echodelay;  // start, stop and echo delay times
    
        if((gettimeofday(&start, NULL)) == -1)
        {
            perror("gettimeofday");
            exit(1);
        }
    
    
        // do stuff
    
    
        if((gettimeofday(&stop, NULL)) == -1)
        {
            perror("gettimeofday");
            exit(1);
        }
    
        /* compute time delay */
        timeval_subtract(&echodelay, &stop, &start);
    
        printf("Echo delay is %ds and %dus\n", echodelay.tv_sec, echodelay.tv_usec);
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>ok i looked thru the glibc manual and found the timeval struct... the code i've come up with is below
    Congratulations. You have a question as well?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    4
    oops

    i forgot to post the question heh

    i meant to ask whether i'm using timeval correctly to compute the time elapsed (as in whether my code is correct)

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Weird Times I'm getting
    By afflictedd2 in forum Linux Programming
    Replies: 8
    Last Post: 07-23-2008, 07:18 AM
  3. What is the best way to record a process execution time?
    By hanash in forum Linux Programming
    Replies: 7
    Last Post: 03-15-2006, 07:17 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM