Created
August 21, 2024 20:12
-
-
Save ShilGen/6e49c70e88bb199badddcc84cbb6cca2 to your computer and use it in GitHub Desktop.
Python3 Decorator for measure time of function's execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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