Skip to content

Instantly share code, notes, and snippets.

@mike-lawrence
Created August 12, 2014 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-lawrence/e541a00ca4f76255e65b to your computer and use it in GitHub Desktop.
Save mike-lawrence/e541a00ca4f76255e65b to your computer and use it in GitHub Desktop.
Simulation of method for increasing precision of time.time() on Windows
for i in `seq 1 100`;
do
python sim.py
done
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