Created
August 12, 2014 16:23
-
-
Save mike-lawrence/e541a00ca4f76255e65b to your computer and use it in GitHub Desktop.
Simulation of method for increasing precision of time.time() on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in `seq 1 100`; | |
do | |
python sim.py | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
clock1 = time.clock() | |
time1 = time.time() | |
true_offset = time1 - clock1 | |
import numpy | |
offsets = [] | |
waits = [] | |
def winTime(): #to simulate 1ms precision | |
return int(time.time()*1000)/1000.0 | |
#collect a bunch of offsets | |
for i in range(1000): | |
last = winTime() | |
clockTime1 = time.clock() | |
this = last | |
while (this-last)==0: | |
this = winTime() | |
clockTime2 = time.clock() | |
offsets.append( this - clockTime2 ) | |
waits.append(clockTime2-clockTime1) | |
waits = numpy.array(waits) | |
offsets = numpy.array(offsets) | |
offset_estimate = offsets[numpy.argmin(numpy.absolute(waits-0.001))] | |
print "%0.30f" % (offset_estimate-true_offset) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment