我有以下脚本,用它测量实际经过的时间,以及几个睡眠功能。我使用time.sleep()暂停程序或psychopy.core.wait(),据说更精确,并使用高分辨率计时器。我正在明确地测试后者,因为wait()函数似乎会导致一些麻烦(例如暂停程序的时间比它应该的短)。
from psychopy import core
import time
import scipy
import sys
times1 = []
times2 = []
times3 = []
times4 = []
testtime = 40 # Time to wait (40 ms)
n = 200 # Iterations
print "Starting timing test with", testtime, "ms as reference; running", n, "times."
for i in range(n):
t1 = time.time()
time.sleep(testtime/1000.0)
measurement = (time.time()-t1)*1000
times1.append(measurement)
time.clock()
time.sleep(testtime/1000.0)
measurement = time.clock()
times2.append(measurement)
t1 = time.time()
core.wait(testtime/1000.0)
measurement = (time.time()-t1)*1000
times3.append(measurement)
t1 = time.clock()
core.wait(testtime/1000.0)
measurement = time.clock()
times4.append(measurement)
if i%60==0:
sys.stdout.write(".")
print
print "Low precision with time.sleep()"
print "Average is", scipy.mean(times1)
print "StdDev is", scipy.std(times1)
print
print "High precision with time.sleep()"
print "Average is", scipy.mean(times2)
print "StdDev is", scipy.std(times2)
print
print "Low precision with PsychoPy core.wait()"
print "Average is", scipy.mean(times3)
print "StdDev is", scipy.std(times3)
print
print "High precision with PsychoPy core.wait()"
print "Average is", scipy.mean(times4)
print "StdDev is", scipy.std(times4)
但我得到的输出是:
Starting timing test with 40 ms as reference; running 200 times.
....
Low precision with time.sleep()
Average is 39.0950024128
StdDev is 7.77598671811
High precision with time.sleep()
Average is 16.2315164609
StdDev is 9.24644085289
Low precision with PsychoPy core.wait()
Average is 40.830000639
StdDev is 21.7002567107
High precision with PsychoPy core.wait()
Average is 16.3130358691
StdDev is 9.24395572035
time.clock()返回的时间太低了!这种情况在我们这里的几个系统中都是一致的。
有没有人知道这里发生了什么以及可能导致这种情况发生了什么?
答案 0 :(得分:1)
您需要对time.time()
执行相同的操作,并在睡眠前保存时钟值并将其减去以进行测量。正如您所拥有的那样,您的所有时钟值都只是测量自过程开始以来的时间。