Skip to content

Instantly share code, notes, and snippets.

@rfong
Last active November 6, 2021 21:02
Show Gist options
  • Save rfong/1cee05cbea98649f1816 to your computer and use it in GitHub Desktop.
Save rfong/1cee05cbea98649f1816 to your computer and use it in GitHub Desktop.
Django profiling decorator
import cProfile
import pstats
from django.utils.functional import wraps
def cprofile(sort_by='cumulative', n=20):
"""Decorator to profile a function."""
def decorator(func):
@wraps(func)
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
_print_profile(profile, sort_by=sort_by, n=n)
return profiled_func
return decorator
def _print_profile(profile, sort_by='cumulative', n=20):
"""Print top profiling results to console."""
pstats.Stats(profile).sort_stats(sort_by).print_stats(n)
def _dump_profile(profile, filename, sort_by='time'):
"""Dump full profiling to file."""
f = open(filename, 'a')
pstats.Stats(profile, stream=f).sort_stats(sort_by).print_stats()
f.close()
@cprofile()
def example():
for i in range(100):
print i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment