Skip to content

Instantly share code, notes, and snippets.

@edgarrmondragon
Created September 24, 2018 23:40
Show Gist options
  • Save edgarrmondragon/660d7766c040b8101443e98a151c3aae to your computer and use it in GitHub Desktop.
Save edgarrmondragon/660d7766c040b8101443e98a151c3aae to your computer and use it in GitHub Desktop.
Python decorator to time function execution
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