Calculating Elapsed Time

Grant Edwards grante at visi.com
Wed Dec 7 12:53:30 EST 2005


On 2005-12-07, Bengt Richter <bokr at oz.net> wrote:

>>In my experience, time.time() on Linux has a resolution of
>>about 1us.  The delta I get when I do
>>
>>  print time.time()-time.time()
>>
>>is usually about 2-3us, but some of that is probably due to the
>>overhead involved.
>>
> Try
>
> >>> import time
> >>> t=time.time; c=time.clock
> >>> min(filter(None,(-float.__sub__(c(),c()) for x in xrange(10000))    ))*1e3
>  0.0058666657878347905
> >>> min(filter(None,(-float.__sub__(t(),t()) for x in xrange(10000))    ))*1e3
>  9.9999904632568359

>>> import time
>>> t=time.time; c=time.clock
>>> min(filter(None,(-float.__sub__(c(),c()) for x in xrange(10000))    ))*1e3
10.000000000000002
>>> min(filter(None,(-float.__sub__(t(),t()) for x in xrange(10000))    ))*1e3
0.00095367431640625

Yup.  That has less overhead than my original example because
you've avoided the extra name lookup:

>>> for f in range(10): 
...  print t()-t()
... 
-4.05311584473e-06
-1.90734863281e-06
-1.90734863281e-06
-2.14576721191e-06
-2.86102294922e-06
-1.90734863281e-06
-2.14576721191e-06
-2.14576721191e-06
-9.53674316406e-07
-1.90734863281e-06

The min delta seen is 0.95us.  I'm guessing thats
function/system call overhead and not timer resolution.

-- 
Grant Edwards                   grante             Yow!  I HAVE a towel.
                                  at               
                               visi.com            



More information about the Python-list mailing list