calculating system clock resolution

Ron Adam rrr at ronadam.com
Sat Apr 8 04:20:15 EDT 2006


Steven D'Aprano wrote:
> On Fri, 07 Apr 2006 16:39:40 -0700, jUrner wrote:
> 
>> Maybe it was not too clear what I was trying to point out.
>>
>> I have to calculate the time time.time() requires to return the next
>> tick of the clock.
>> Should be about 0.01ms but this may differ from os to os.
> 
> I suspect that Python isn't quite fast enough to give an accurate measure
> for this, but I could be wrong.

>>>> import time
>>>>
>>>> calc_time_res()
> 2.50339508057e-05
>>>> calc_time_res()
> 2.59876251221e-05
>>>> calc_time_res()
> 2.59876251221e-05
>>>> calc_time_res()
> 2.59876251221e-05
>>>> calc_time_res()
> 2.40802764893e-05

Trying this on my win xp gives the following.


0.0150001049042        # time.time()

2.23492091872e-006     # time.clock()

2.7936511484e-006
1.95555580388e-006     #<- lowest value for time.clock()
1.95555580388e-006
1.95555580388e-006
3.35238137808e-006
1.95555580388e-006
1.95555580388e-006
1.95555580388e-006
2.23492091872e-006

But I think this is going to vary from system to system as well as what 
os is used.  And it may be effected by other tasks as well so checking 
multiple times is probably needed.


def calc_time_res():
     now = time.time
     start = now()
     x = start
     while start == x:
         x = now()
     print x - start

def calc_time_res2():
     now = time.clock
     start = now()
     x = start
     while start == x:
         x = now()
     print x - start

def calc_time_res3():
     now = time.clock
     r = range(10)
     times = [now() for x in r]
     start = times[0]
     for x in times[1:]:
         print x-start
         start = x

calc_time_res()
print
calc_time_res2()
print
calc_time_res3()






More information about the Python-list mailing list