-
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);
}
-
>>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?
-
oops :D
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