Created
September 24, 2018 23:40
-
-
Save edgarrmondragon/660d7766c040b8101443e98a151c3aae to your computer and use it in GitHub Desktop.
Python decorator to time function execution
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
import time | |
def timefunc(f, *args, **kwargs): | |
time1 = time.time() | |
result = f(*args, **kwargs) | |
time2 = time.time() | |
elapsed = time2 - time1 | |
return result, elapsed | |
@timefunc | |
def loop(): | |
s = 0 | |
for i in range(1000000): | |
s += 1 | |
return s | |
print(loop) # (1000000, 0.13299798965454102) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment