Skip to content

Instantly share code, notes, and snippets.

@akarsh1995
Created August 1, 2020 17:40
Show Gist options
  • Select an option

  • Save akarsh1995/65024ed157ab16f6bd7b71689a171d41 to your computer and use it in GitHub Desktop.

Select an option

Save akarsh1995/65024ed157ab16f6bd7b71689a171d41 to your computer and use it in GitHub Desktop.
Timeit ⏳ decorator to measure the execution speed of the python function. Use anywhere you like...🐍
import time
def timeit(method):
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
kw['log_time'][name] = int((te - ts) * 1000)
else:
print(f'{method.__name__} func'
f' took {((te - ts) * 1000):.2f} ms')
return result
return timed
@timeit
def sleeping():
time.sleep(5)
if __name__ == '__main__':
sleeping()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment