Skip to content

Instantly share code, notes, and snippets.

@ShilGen
Created August 21, 2024 20:12
Show Gist options
  • Save ShilGen/6e49c70e88bb199badddcc84cbb6cca2 to your computer and use it in GitHub Desktop.
Save ShilGen/6e49c70e88bb199badddcc84cbb6cca2 to your computer and use it in GitHub Desktop.
Python3 Decorator for measure time of function's execution
import time
def time_it(func):
def wrapper(*args,**kwargs):
start_time = time.time()
result =func(*args, **kwargs)
end_time = time.time()
print(f"Function {func.__name__} took {end_time - start_time:.4f} seconds")
return result
return wrapper
@time_it
def compute_square(n):
return [i * i for i in range(n)]
compute_square(1234567)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment