Skip to content

Instantly share code, notes, and snippets.

@pratos
Created October 14, 2018 03:01
Show Gist options
  • Save pratos/45dd1b484f40925c57a71b6185cb509a to your computer and use it in GitHub Desktop.
Save pratos/45dd1b484f40925c57a71b6185cb509a to your computer and use it in GitHub Desktop.
Decorator: TImeit
def timeit(method):
def timing(*args, **kwargs):
timings = []
print("Running this 1000 loops, for benchmarking")
for i in range(1000):
start = time.time()
result = method(*args, **kwargs)
end = time.time()
timings.append(end-start)
average = np.average(timings)
std_dev = statistics.stdev(timings)
print("{:.2f} ms ± {:.3f} ms average time taken per loop (mean ± std. dev. to run 1000 loops)"\
.format(average*1000, std_dev*1000))
return(result)
return(timing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment