我想用Python编写自己的时钟对象.我希望它非常非常准确.我在Windows上看到,我可以使用QueryPerformanceCounter().但是怎么样?我不知道任何C; 只有Python 2.x.
有人可以给我一个提示,如何在Python中使用它来在Win上制作一个准确的时钟吗?
我在这个论坛上得到了一些关于如何在 Python 2 中编写时钟对象的很好的提示。我现在有一些代码可以工作。这是一个以 60 FPS“滴答”的时钟:
import sys
import time
class Clock(object):
def __init__(self):
self.init_os()
self.fps = 60.0
self._tick = 1.0 / self.fps
print "TICK", self._tick
self.check_min_sleep()
self.t = self.timestamp()
def init_os(self):
if sys.platform == "win32":
self.timestamp = time.clock
self.wait = time.sleep
def timeit(self, f, args):
t1 = self.timestamp()
f(*args)
t2 = self.timestamp()
return t2 - t1
def check_min_sleep(self):
"""checks the min sleep time on the system"""
runs = 1000
times = [self.timeit(self.wait, (0.001, )) for n in xrange(runs)]
average …Run Code Online (Sandbox Code Playgroud)