Created
December 6, 2019 20:18
-
-
Save MagnetonBora/90adaa1f9158ee74868b627c7d1134b2 to your computer and use it in GitHub Desktop.
This file contains 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
# This program will demonstrate how we can use python decorators | |
# to measure the time of function invoking | |
import time | |
def chrono(fn): | |
def wrapper(*args): | |
start = time.time() # get current time before running the function | |
result = fn(*args) # invoking function | |
duration = time.time() - start # measuring the duration | |
print(f'The total time for function invoking is {duration} seconds') | |
return result | |
return wrapper | |
@chrono | |
def run(timeout=5): | |
print(f'Invoking function run with timeout {timeout} second') | |
time.sleep(timeout) | |
def main(): | |
run() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment