Created
September 27, 2023 02:49
-
-
Save h3ct0rjs/f55e1540e3e601bde04b7c87246a939a to your computer and use it in GitHub Desktop.
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 | |
# Define a decorator function to measure execution time | |
def timing_decorator(func): | |
def wrapper(*args, **kwargs): | |
start_time = time.time() | |
result = func(*args, **kwargs) | |
end_time = time.time() | |
execution_time = end_time - start_time | |
print(f"{func.__name__} took {execution_time:.2f} seconds to execute.") | |
return result | |
return wrapper | |
# Apply the timing decorator to a function | |
@timing_decorator | |
def slow_function(): | |
time.sleep(2) # Simulate a slow operation | |
# Call the decorated function | |
slow_function() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment