PEP 418: clock_precision.py was wrong, recompute all "Precision in Python"
This commit is contained in:
parent
017c74ca0c
commit
0dcc3e959d
|
@ -1,26 +1,33 @@
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
try:
|
||||||
|
from time import timeout_time
|
||||||
|
except ImportError:
|
||||||
|
from time import time as timeout_time
|
||||||
|
|
||||||
def compute_precision(func):
|
def compute_precision(func):
|
||||||
previous = func()
|
|
||||||
precision = None
|
precision = None
|
||||||
points = 0
|
points = 0
|
||||||
min_points = 100
|
timeout = timeout_time() + 1.0
|
||||||
while points < min_points:
|
previous = func()
|
||||||
value = func()
|
while timeout_time() < timeout or points < 3:
|
||||||
if value == previous:
|
for loop in range(10):
|
||||||
continue
|
t1 = func()
|
||||||
dt = value - previous
|
t2 = func()
|
||||||
previous = value
|
dt = t2 - t1
|
||||||
|
if 0 < dt:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
dt = t2 - previous
|
||||||
|
if dt <= 0.0:
|
||||||
|
continue
|
||||||
if precision is not None:
|
if precision is not None:
|
||||||
precision = min(precision, dt)
|
precision = min(precision, dt)
|
||||||
else:
|
else:
|
||||||
precision = dt
|
precision = dt
|
||||||
if precision < 10e-6:
|
|
||||||
min_points = 5000
|
|
||||||
elif precision < 1e-3:
|
|
||||||
min_points = 1000
|
|
||||||
points += 1
|
points += 1
|
||||||
return dt
|
previous = func()
|
||||||
|
return precision
|
||||||
|
|
||||||
def format_duration(dt):
|
def format_duration(dt):
|
||||||
if dt >= 1e-3:
|
if dt >= 1e-3:
|
||||||
|
@ -31,8 +38,8 @@ def format_duration(dt):
|
||||||
return "%.0f ns" % (dt * 1e9)
|
return "%.0f ns" % (dt * 1e9)
|
||||||
|
|
||||||
def test_clock(name, func):
|
def test_clock(name, func):
|
||||||
precision = compute_precision(func)
|
|
||||||
print("%s:" % name)
|
print("%s:" % name)
|
||||||
|
precision = compute_precision(func)
|
||||||
print("- precision in Python: %s" % format_duration(precision))
|
print("- precision in Python: %s" % format_duration(precision))
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,6 +57,7 @@ for name in clocks:
|
||||||
print("- resolution: %s" % format_duration(info['resolution']))
|
print("- resolution: %s" % format_duration(info['resolution']))
|
||||||
|
|
||||||
clock_ids = [name for name in dir(time) if name.startswith("CLOCK_")]
|
clock_ids = [name for name in dir(time) if name.startswith("CLOCK_")]
|
||||||
|
clock_ids.sort()
|
||||||
for clock_id_text in clock_ids:
|
for clock_id_text in clock_ids:
|
||||||
clock_id = getattr(time, clock_id_text)
|
clock_id = getattr(time, clock_id_text)
|
||||||
name = 'clock_gettime(%s)' % clock_id_text
|
name = 'clock_gettime(%s)' % clock_id_text
|
||||||
|
|
Loading…
Reference in New Issue