Skip to content

Instantly share code, notes, and snippets.

@shreewatsa
Created July 7, 2023 00:52
Show Gist options
  • Save shreewatsa/d93a9f8c15d88041cb3cdcca024266a9 to your computer and use it in GitHub Desktop.
Save shreewatsa/d93a9f8c15d88041cb3cdcca024266a9 to your computer and use it in GitHub Desktop.
How to profile python applications ?

Profiling Python applications.

  1. Profiling time (time complexity) : Scalene, Yappi, pprofile, Snakeviz, Pyinstrument, py-spy, etc.
  2. Profiling memory (space complexity) : memory_profiler, guppy/hpy, tracemalloc, Scalene, Pympler, etc.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment