Skip to content

Instantly share code, notes, and snippets.

@verystrongjoe
Created December 3, 2022 09:52
Show Gist options
  • Save verystrongjoe/7b47696007b4ad3934860c79d4494f1d to your computer and use it in GitHub Desktop.
Save verystrongjoe/7b47696007b4ad3934860c79d4494f1d to your computer and use it in GitHub Desktop.
simple timeit annotation
from functools import wraps
import time
def timeit(func):
@wraps(func)
def timeit_wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
total_time = end_time - start_time
# print(f'Function {func.__name__}{args} {kwargs} Took {total_time:.4f} seconds')
print(f'Function {func.__name__} Took {total_time:.4f} seconds')
return result
return timeit_wrapper
@timeit
def calculate_something(num):
"""
Simple function that returns sum of all numbers up to the square of num.
"""
total = sum((x for x in range(0, num**2)))
return total
if __name__ == '__main__':
calculate_something(10)
calculate_something(100)
calculate_something(1000)
calculate_something(5000)
calculate_something(10000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment