Skip to content

Instantly share code, notes, and snippets.

@philippstroehle
Created September 16, 2013 20:46
Show Gist options
  • Save philippstroehle/6586379 to your computer and use it in GitHub Desktop.
Save philippstroehle/6586379 to your computer and use it in GitHub Desktop.
Profiling runtime and memory
@profile # python -m memory_profiler example.py
def my_func():
a = [1] * (10 ** 6)
b = [2] * (2 * 10 ** 7)
del b
return a
if __name__ == '__main__':
my_func()
if False:
main()
else:
import cProfile
# call method main and log results to profile.log
cProfile.run('main()', 'profile.log')
import pstats
# used to analyze stats
p = pstats.Stats('profile.log')
# return the 100 most time consuming methods
p.sort_stats('time').print_stats(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment