How to make a repeatedly timer with 5ms precision?

卢剑雄 jxlu at fudan.edu.cn
Wed Apr 30 00:10:49 EDT 2003


I am trying to make a repeatedly timer with high precision, the following program 'countdown.py' is an example which shows the way how I tried to make it.
The problem is that it doesn't work properly with tick set to 5ms or smaller.
Is there a better way to implement a more precise repeatedly timer?
C:\>type countdown.py
#Syntax: countdown [tickle [count]]

import sys
import threading
import time

tick=1.0
count=5
argc=len(sys.argv)
if argc==1:
    pass
elif argc==2:
    tick=float(sys.argv[1])
elif argc==3:
    tick=float(sys.argv[1])
    count=int(sys.argv[2])
else:
    print 'Syntax: countdown [tickle [count]]'
    sys.exit()
if tick<=0.0 or count<0:
    print 'Condition: tick<=0.0 or count<0 are not allowed.'
    sys.exit()
def countdown(tick,count):
    if count>0:
        t=threading.Timer(tick,countdown,[tick,count-1])
        t.start()
        print count,time.time()
    elif count==0:
        end=time.time()
        print count,end
        print 'end-start=',end-start
t=threading.Timer(tick,countdown,[tick,count-1])
start=time.time()
print count,start
t.start()

C:\>countdown.py 1.0 10
10 1051675124.74
9 1051675125.75
8 1051675126.75
7 1051675127.75
6 1051675128.75
5 1051675129.76
4 1051675130.76
3 1051675131.76
2 1051675132.76
1 1051675133.76
0 1051675134.76
end-start= 10.0230000019

C:\>countdown.py 1.0 10
10 1051675137.52
9 1051675138.52
8 1051675139.52
7 1051675140.52
6 1051675141.52
5 1051675142.52
4 1051675143.52
3 1051675144.52
2 1051675145.53
1 1051675146.53
0 1051675147.53
end-start= 10.0130000114

C:\>countdown.py 0.1 10
10 1051675155.99
9 1051675156.09
8 1051675156.19
7 1051675156.3
6 1051675156.4
5 1051675156.5
4 1051675156.6
3 1051675156.7
2 1051675156.8
1 1051675156.9
0 1051675157.0
end-start= 1.01100003719

C:\>countdown.py 0.1 10
10 1051675160.88
9 1051675160.99
8 1051675161.09
7 1051675161.19
6 1051675161.3
5 1051675161.41
4 1051675161.51
3 1051675161.61
2 1051675161.71
1 1051675161.81
0 1051675161.91
end-start= 1.03199994564

C:\>countdown.py 0.01 10
10 1051675172.42
9 1051675172.43
8 1051675172.44
7 1051675172.46
6 1051675172.47
5 1051675172.48
4 1051675172.49
3 1051675172.5
2 1051675172.51
1 1051675172.52
0 1051675172.53
end-start= 0.110999941826

C:\>countdown.py 0.01 10
10 1051675176.4
9 1051675176.41
8 1051675176.42
7 1051675176.43
6 1051675176.44
5 1051675176.45
4 1051675176.47
3 1051675176.48
2 1051675176.49
1 1051675176.5
0 1051675176.51
end-start= 0.110000014305

C:\>countdown.py 0.005 10
10 1051675213.6
9 1051675213.61
8 1051675213.62
7 1051675213.63
6 1051675213.64
5 1051675213.65
4 1051675213.66
3 1051675213.67
2 1051675213.68
1 1051675213.69
0 1051675213.7
end-start= 0.100000023842

C:\>countdown.py 0.005 10
10 1051675217.88
9 1051675217.89
8 1051675217.9
7 1051675217.91
6 1051675217.92
5 1051675217.93
4 1051675217.94
3 1051675217.95
2 1051675217.96
1 1051675217.97
0 1051675217.98
end-start= 0.100000023842

C:\>





More information about the Python-list mailing list