Here is a small code for python
Code:
import time
from math import sqrt
def getMedian(numericValues): # From http://codecomments.wordpress.com/2008/03/17/simple-method-to-calculate-median-in-python/
theValues = sorted(numericValues)
if len(theValues) % 2 == 1:
return theValues[(len(theValues)+1)/2-1]
else:
lower = theValues[len(theValues)/2-1]
upper = theValues[len(theValues)/2]
return (float(lower + upper)) / 2
def meanstdv(x): # From http://www.phys.uu.nl/~haque/computing/WPark_recipes_in_python.html
n, mean, std = len(x), 0, 0
for a in x:
mean = mean + a
mean = mean / float(n)
for a in x:
std = std + (a - mean)**2
std = sqrt(std / float(n-1))
return mean, std
def _TimeIt(func,*args,**kwargs):
s_time = time.time()
func(*args,**kwargs)
e_time = time.time()
return e_time - s_time
def TimeIt(func,*args,**kwargs):
print "Execution time %f seconds" % _TimeIt(func,*args,**kwargs)
def TimeItStat(n,func,*args,**kwargs):
if n <= 0: raise ValueError("n must be greater than zero")
times = []
for i in range(n):
times.append( _TimeIt(func,*args,**kwargs) )
mean, sd = meanstdv(times)
med = getMedian(times)
print "Execution time mean=%f med=%f best=%f worst=%f sd=%f" % (mean,med,min(times),max(times),sd)
# Example usage
def main(runTimes,hello=""):
for i in range(runTimes):
hello += hello
if __name__ == "__main__":
TimeIt(main,25,hello="world")
TimeItStat(10,main,25,hello="world")
And the output
$ python timeit.py
Execution time 0.465171 seconds
Execution time mean=0.052075 med=0.468760 best=0.468105 worst=0.469439 sd=0.205118