2012-04-13 16:07:36 -04:00
|
|
|
import time
|
|
|
|
|
2012-04-18 16:49:58 -04:00
|
|
|
try:
|
|
|
|
from time import timeout_time
|
|
|
|
except ImportError:
|
|
|
|
from time import time as timeout_time
|
|
|
|
|
2012-04-13 16:07:36 -04:00
|
|
|
def compute_precision(func):
|
|
|
|
precision = None
|
|
|
|
points = 0
|
2012-04-18 16:49:58 -04:00
|
|
|
timeout = timeout_time() + 1.0
|
|
|
|
previous = func()
|
|
|
|
while timeout_time() < timeout or points < 3:
|
|
|
|
for loop in range(10):
|
|
|
|
t1 = func()
|
|
|
|
t2 = func()
|
|
|
|
dt = t2 - t1
|
|
|
|
if 0 < dt:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
dt = t2 - previous
|
|
|
|
if dt <= 0.0:
|
|
|
|
continue
|
2012-04-13 16:07:36 -04:00
|
|
|
if precision is not None:
|
|
|
|
precision = min(precision, dt)
|
|
|
|
else:
|
|
|
|
precision = dt
|
|
|
|
points += 1
|
2012-04-18 16:49:58 -04:00
|
|
|
previous = func()
|
|
|
|
return precision
|
2012-04-13 16:07:36 -04:00
|
|
|
|
|
|
|
def format_duration(dt):
|
|
|
|
if dt >= 1e-3:
|
2012-04-13 17:24:44 -04:00
|
|
|
return "%.0f ms" % (dt * 1e3)
|
2012-04-13 16:07:36 -04:00
|
|
|
if dt >= 1e-6:
|
2012-04-17 17:40:47 -04:00
|
|
|
return "%.0f us" % (dt * 1e6)
|
2012-04-13 16:07:36 -04:00
|
|
|
else:
|
2012-04-13 17:24:44 -04:00
|
|
|
return "%.0f ns" % (dt * 1e9)
|
2012-04-13 16:07:36 -04:00
|
|
|
|
|
|
|
def test_clock(name, func):
|
|
|
|
print("%s:" % name)
|
2012-04-18 16:49:58 -04:00
|
|
|
precision = compute_precision(func)
|
2012-04-13 16:07:36 -04:00
|
|
|
print("- precision in Python: %s" % format_duration(precision))
|
|
|
|
|
|
|
|
|
2012-04-17 18:39:39 -04:00
|
|
|
clocks = ['clock', 'perf_counter', 'process_time']
|
2012-04-17 18:39:03 -04:00
|
|
|
if hasattr(time, 'monotonic'):
|
|
|
|
clocks.append('monotonic')
|
|
|
|
clocks.append('time')
|
|
|
|
for name in clocks:
|
2012-04-13 16:07:36 -04:00
|
|
|
func = getattr(time, name)
|
|
|
|
test_clock("%s()" % name, func)
|
|
|
|
info = time.get_clock_info(name)
|
|
|
|
if 'precision' in info:
|
|
|
|
print("- announced precision: %s" % format_duration(info['precision']))
|
2012-04-13 17:24:44 -04:00
|
|
|
print("- implementation: %s" % info['implementation'])
|
2012-04-13 16:07:36 -04:00
|
|
|
print("- resolution: %s" % format_duration(info['resolution']))
|
|
|
|
|
|
|
|
clock_ids = [name for name in dir(time) if name.startswith("CLOCK_")]
|
2012-04-18 16:49:58 -04:00
|
|
|
clock_ids.sort()
|
2012-04-13 16:07:36 -04:00
|
|
|
for clock_id_text in clock_ids:
|
|
|
|
clock_id = getattr(time, clock_id_text)
|
|
|
|
name = 'clock_gettime(%s)' % clock_id_text
|
|
|
|
def gettime():
|
|
|
|
return time.clock_gettime(clock_id)
|
2012-04-17 17:59:33 -04:00
|
|
|
try:
|
|
|
|
gettime()
|
|
|
|
except OSError as err:
|
|
|
|
print("%s failed: %s" % (name, err))
|
|
|
|
continue
|
2012-04-13 16:07:36 -04:00
|
|
|
test_clock(name, gettime)
|
2012-04-13 18:47:00 -04:00
|
|
|
precision = time.clock_getres(clock_id)
|
|
|
|
print("- announced precision: %s" % format_duration(precision))
|
2012-04-13 16:07:36 -04:00
|
|
|
|