from __future__ import print_function
import time
import subprocess
import sys
devnull = open('/dev/null', 'w')
def run(cmd, repeat=10):
print('sampling:', end=' ')
sys.stdout.flush()
samples = []
for _ in range(repeat):
start = time.time()
subprocess.call(cmd, stdout=devnull, stderr=devnull)
end = time.time()
dt = (end - start) * 1000
print('%dms' % int(dt), end=' ')
sys.stdout.flush()
samples.append(dt)
print()
best = min(samples)
err = sum(s - best for s in samples) / float(len(samples))
print('estimate: %dms (mean err %.1fms)' % (best, err))
if __name__ == '__main__':
if len(sys.argv) < 2:
print('usage: measure.py command args...')
sys.exit(1)
run(cmd=sys.argv[1:])