[uClibc] clock() for long-running process
Joakim Tjernlund
joakim.tjernlund at lumentis.se
Mon May 10 14:15:49 UTC 2004
> >>>>> On Sun, 9 May 2004 15:41:23 +0200, "Joakim Tjernlund" <Joakim.Tjernlund at lumentis.se> said:
>
> Tjernlund> hmm, times() returns a clock_t type which is signed. How
> Tjernlund> should one use times() to calc. elapsed time? Just type
> Tjernlund> cast its return value to an unsigned long?
>
> I think just taking difference of return values of two times() call
> will be enough. (at least in short range)
But clock_t is signed so that can't work, can it?
clock_t t1,t2,diff; /* clock_t is a signed long */
t1 = times(NULL);
/* do work */
t2 = times(NULL);
diff = t2 - t1;
Suppose times() wrap to a negative value between t1 and t2. Then
you have:
diff = t2 - t1 = -x(t2) - y(t1) = < 0, right?
Assuming libc don't mess with the return value from times(), the
very least you have to do is to have t1, t2, and diff as unsigned long:
unsigned long t1,t2,diff;
t1 = (unsigned long)times(NULL);
/* do work */
t2 = (unsigned long)times(NULL);
diff = t2 - t1;
Is this enough or is more magic needed?
More information about the uClibc
mailing list