1

我有以下脚本,我用它来测量几个睡眠函数的实际经过时间。我使用 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() 返回的时间太低了!这在我们这里拥有的几个系统中始终如一地发生。

有没有人知道这里发生了什么以及可能导致这种情况的原因?

4

1 回答 1

1

你需要做同样的事情,time.time()在你睡觉之前保存时钟值,然后减去它来得到你的测量值。正如你所拥有的那样,你所有的时钟值都只是在测量过程开始以来的时间。

于 2013-09-25T11:18:02.063 回答