Skip to content

Instantly share code, notes, and snippets.

@henriquerichter
Last active August 20, 2018 23:10
Show Gist options
  • Select an option

  • Save henriquerichter/fd4ceb7426d1173e2e864c8ff109d474 to your computer and use it in GitHub Desktop.

Select an option

Save henriquerichter/fd4ceb7426d1173e2e864c8ff109d474 to your computer and use it in GitHub Desktop.
import time
class Benchmark():
def __init__(self, txt):
self.txt = str(txt)
self.start = time.time()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
end = time.time()
print(self.txt + " took " + str(end - self.start) + " seconds.")
def example_function():
time.sleep(5)
if __name__ == '__main__':
with Benchmark("example_function"):
example_function()
import time
def benchmark(func):
def _timer(*args, **kwargs):
start = time.time()
value = func(*args, **kwargs)
end = time.time()
print(str(func.__name__) + " took " + str(end - start) + " seconds.")
return value
return _timer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment