apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brian Pane <bri...@apache.org>
Subject Re: Why not POSIX time_t?
Date Mon, 15 Jul 2002 02:42:09 GMT
Aaron Bannert wrote:

>>The reason we don't use a struct (timeval or any variant thereof)
>>is that doing addition and subtraction on the struct is much slower,
>>more complicated, and (if people try to do their own match on the
>>struct directly) more error-prone than doing the same ops on a
>>scalar.
>>    
>>
>
>How exactly is the subtraction slower?
>

Subtraction with a scalar:

   delta = time1 - time2;

Subtraction with a struct timeval:

   if (time1.tv_usec < time2.tv_usec) {
       delta = 1000000*(time1.tv_sec - 1 - time2.tv_sec) +
               (1000000 + time1.tv_usec - time2.tv_usec);
   }
   else {
       delta = 1000000*(time1.tv_sec - time2.tv_sec) +
               (time1.tv_usec - time2.tv_usec);
   }

It's a little more efficient if you put the result in
a struct rather than a scalar, but you still have to do
the carry from the seconds field to the microseconds field
if time1.tv_usec < time2.tv_usec.  Minimally, subtraction
of the structs requires two subtractions, a comparison,
and a conditional branch.

>I'm not at all sure what you
>mean by people matching on the struct directly...
>

That was a typo; it should have said "if people
try to do their own math."

--Brian



Mime
View raw message