Skip to content

Instantly share code, notes, and snippets.

@Gautier
Created August 7, 2012 14:49
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 Gautier/3285964 to your computer and use it in GitHub Desktop.
Save Gautier/3285964 to your computer and use it in GitHub Desktop.
Benchmark arbitrary command n times
#!/usr/bin/env python
import subprocess
import sys
import time
def avg(nums):
return sum(nums) / len(nums)
def main():
if len(sys.argv) < 3:
print "Usage: mini_bench N arg1 arg2"
return
n = int(sys.argv[1])
cmd = sys.argv[2:]
times = []
for i in range(n):
t0 = time.time()
ret = subprocess.call(cmd)
assert ret == 0
times.append(time.time() - t0)
print "Called the command %d times" % n
print "Average time : %f" % avg(times)
print "Best time : %f" % min(times)
print "Worst time : %f" % max(times)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment