Calculating Elapsed Time

Grant Edwards grante at visi.com
Wed Dec 7 13:32:50 EST 2005


On 2005-12-07, Fredrik Lundh <fredrik at pythonware.com> wrote:

>> import time
>> for i in range(10):
>>     print time.time()-time.time()
>>
>> After the first loop, I usually get one of three values:
>>
>>   3.099us, 2.14,us, 2.86us.
>
> I get two different values:
>
> -1.90734863281e-06
> -2.14576721191e-06
>
> on this hardware (faster than the PC I'm using right now, but still not a
> very fast machine).  let's check a faster linux box:
>
> $ python2.4 test.py
> -6.91413879395e-06
> -1.90734863281e-06
> -1.90734863281e-06
> -1.90734863281e-06
> -1.90734863281e-06
> -2.14576721191e-06
> -1.90734863281e-06
> -2.14576721191e-06
> -1.90734863281e-06
> -1.90734863281e-06
>
> if I keep running the script over and over again, I do get individual
>
> -1.19209289551e-06
>
> items from time to time on both machines...

We're seeing floating point representation issues.  

The resolution of the underlying call is exactly 1us.  Calling
gettimeofday() in a loop in C results in deltas of exactly 1 or
2 us.  Python uses a C double to represent time, and a double
doesn't have enough bit to accurately represent 1us resolution.

-- 
Grant Edwards                   grante             Yow!  I'm a fuschia bowling
                                  at               ball somewhere in Brittany
                               visi.com            



More information about the Python-list mailing list