Python time.clock() result not precise and way off
Question: I have the following script with which I measure real elapsed time forseveral
sleep functions. I use either time.sleep() to pause the program or
psychopy.core.wait() which is said to be more precise and use the high
resolution timer. I'm testing the latter explicitly because the wait()
function appears to cause some trouble (e.g. pauses the program shorter than
it should).

    
    
    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)
    

The output I get however is:

    
    
    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
    

The time returned by time.clock() is way too low! And this happens consistenly
across several systems we have here.

Is there anyone who has an idea what is going on here and what might cause
this?


Answer: You need to do the same as you are doing for `time.time()` and save the clock
value before you do your sleep and subtract it to get your measurement. As you
have it all your clock values are just measuring time since the process
started.

