function timer
Sun Oct 09 2022 07:13:24 GMT+0000 (UTC)
Saved by
@raymondfdavey
#vscode
#python
import time
import numpy as np
def timeit(somefunc,*args,repeats=100,**kwargs):
times=[] # initialise array to collect the time for each repetition
for i in range(repeats): # set loop for number of repetitions to run and time the function that many times
starttime=time.time() # set start
ans=somefunc(*args,**kwargs) # run the function
endtime=time.time() # set end
timetaken=endtime-starttime # calculate time from start and end
times.append(timetaken) # append time for this run to the times list
mean=np.mean(times) # calculate the average time across the repetitions
stdev=np.std(times) # get the standard deviation of the times
error=stdev/(repeats**0.5) # get the error?
return (ans,mean,error)
content_copyCOPY
Comments