Skip to content

Instantly share code, notes, and snippets.

@un33k
Last active November 14, 2023 16:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save un33k/6134293 to your computer and use it in GitHub Desktop.
Save un33k/6134293 to your computer and use it in GitHub Desktop.
Performance Profile Decorator (function wrapper based on cProfile). Decorate any function to profile it.
import cProfile
def profileit(func):
"""
Decorator (function wrapper) that profiles a single function
@profileit()
def func1(...)
# do something
pass
"""
def wrapper(*args, **kwargs):
func_name = func.__name__ + ".pfl"
prof = cProfile.Profile()
retval = prof.runcall(func, *args, **kwargs)
prof.dump_stats(func_name)
return retval
return wrapper
# Example
@profileit
def foo():
a = 0
for b in range(1, 100000):
a += b
foo()
# Analysis
#python -m pstats foo.pfl
#Welcome to the profile statistics browser.
#foo.pfl% help
#foo.pfl% sort time
#foo.pfl% stats 10
#Fri Mar 14 10:26:03 2014 foo.pfl
#
# 7 function calls in 0.008 seconds
#
# Ordered by: internal time
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.007 0.007 0.007 0.007 {method 'enable' of '_lsprof.Profiler' objects}
# 1 0.002 0.002 0.008 0.008 profile.py:1(<module>)
# 1 0.000 0.000 0.000 0.000 C:\Python27\lib\cProfile.py:5(<module>)
# 1 0.000 0.000 0.007 0.007 profile.py:12(wrapper)
# 1 0.000 0.000 0.007 0.007 C:\Python27\lib\cProfile.py:146(runcall)
# 1 0.000 0.000 0.000 0.000 C:\Python27\lib\cProfile.py:66(Profile)
# 1 0.000 0.000 0.000 0.000 profile.py:3(profileit)
@gbartz
Copy link

gbartz commented Apr 25, 2018

Just what I needed,
Thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment