UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2002-09-09 12:34 PM

redemption
Member
Registered: 2002-09-09
Posts: 3

Re: computing time elapsed in C?

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

/**
 * 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);
}

Offline

#2 2002-09-09 01:31 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: computing time elapsed in C?

And, what is your question?

I'm not sure that code is entirely correct...

In <sys/time.h> on my system (using glibc 2.1.x), there's a
timersub() macro designed to do exactly what you're trying to do
with your function...

Offline

#3 2002-09-09 02:21 PM

redemption
Member
Registered: 2002-09-09
Posts: 3

Re: computing time elapsed in C?

oops sorry if i wasn't clear...

i meant to ask if the code below is correct for computing the time elapsed...

why isn't it correct?

and there is a timesub() macro that does this? where can i read about... i can't find it in my manual? what do you use to look up things like that?

thanks (again)

Offline

#4 2002-09-09 08:02 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: computing time elapsed in C?

What I used to look it up was "less /usr/include/sys/time.h"... ;-)  I
never trust man pages (or other forms of documentation) too far...
Not only are they sometimes misleading (or wildly inaccurate), but
they rarely document more than a small portion of available
functions/macros...  Always go digging through your header files;
you'll sometimes be amazed at the amazingly cool stuff you find... ;-)

As for your implementation, oh I don't know that it really isn't correct;
it just looked odd to me... ;-)  Like it was doing more than was called
for...  I really didn't feel like trying to think it through, though... ;-)
But, ok, let's look at it...  Why the division by "1000000 + 1"??  Where
did you pull that from??  As near as I can tell, that's ALWAYS going
to give you an "nsec" value of 0...  ("tv_usec" should never have a
value above 1000000, so subtracting one from another should
certainly never yield a value greater than 1000000...)  And, though
I thought this is what you were trying to accomplish with that logic,
it doesn't really look to me as if it will properly handle the case of
having a larger tv_usec value on the right side of your subtraction...
Eg: x->tv_sec = 5, x->tv_usec = 1, y->tv_sec = 3, y->tv_usec = 999999;
your function would yield result->tv_sec = 2, result->tv_usec = -999998,
when in fact, you want result->tv_sec = 1, result->tv_usec = 2...

Offline

#5 2002-09-09 11:03 PM

mlampkin
Administrator
From: Sol 3
Registered: 2002-06-12
Posts: 911
Website

Re: computing time elapsed in C?

The following are the definitions for the macros that Rob mentioned:

#ifdef __USE_BSD
/* Convenience macros for operations on timevals.
   NOTE: `timercmp' does not work for >= or <=.  */

# define timercmp(a, b, CMP)                                                   \
  (((a)->tv_sec == (b)->tv_sec) ?                                              \
   ((a)->tv_usec CMP (b)->tv_usec) :                                     \
   ((a)->tv_sec CMP (b)->tv_sec))

# define timeradd(a, b, result)                                                    \
  do {                                                                                                       \
    (result)->tv_sec = (a)->tv_sec + (b)->tv_sec;                \
    (result)->tv_usec = (a)->tv_usec + (b)->tv_usec;        \
    if ((result)->tv_usec >= 1000000)                                       \
      {                                                                                                         \
        ++(result)->tv_sec;                                                               \
        (result)->tv_usec -= 1000000;                                          \
      }                                                                                                        \
  } while (0)

# define timersub(a, b, result)                                                   \
  do {                                                                                                      \
    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;                \
    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;        \
    if ((result)->tv_usec < 0) {                                                     \
      --(result)->tv_sec;                                                                  \
      (result)->tv_usec += 1000000;                                          \
    }                                                                                                         \
  } while (0)

#endif  /* BSD */


Michael


"The only difference between me and a madman is that I'm not mad."

Salvador Dali (1904-1989)

Offline

#6 2002-09-09 11:53 PM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: computing time elapsed in C?

I wonder if someone was trying to add some ASCII art or some special ecoded message in the spacing of those "\" line continuations in those macros Michael pasted--laughs!

Offline

#7 2002-09-10 12:39 PM

redemption
Member
Registered: 2002-09-09
Posts: 3

Re: computing time elapsed in C?

Offline

Board footer

Powered by FluxBB