Skip to content

Instantly share code, notes, and snippets.

@richardARPANET
Created May 29, 2019 12:50
Show Gist options
  • Save richardARPANET/fd4aaaf074d364eefe138b839683277b to your computer and use it in GitHub Desktop.
Save richardARPANET/fd4aaaf074d364eefe138b839683277b to your computer and use it in GitHub Desktop.
Profile any Python function easily.
from functools import wraps
import cProfile
def profileit(func):
@wraps(func)
def wrapper(*args, **kwargs):
profile = cProfile.Profile()
return_value = profile.runcall(func, *args, **kwargs)
profile.dump_stats(f'{func.__name__}.profile')
return return_value
return wrapper
# then read the output with snakeviz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment