123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- # profile/cProfile
- John Melesky
- (PDX Python, May 2010)
- ---
- # In the standard library
- - hotshot
- - timeit
- - profile/cprofile/pstats
- ---
- # hotshot
- ---
- # timeit
- ---
- # profile/cProfile
- Instead of:
- if __name__ == '__main__':
- main()
- Do:
- if __name__ == '__main__':
- import cProfile
- cProfile.run('main()')
- ---
- # Questions?
- ---
- # Need more detail!
- ---
- # Need more detail!
- Instead of:
- if __name__ == '__main__':
- import cProfile
- cProfile.run('main()')
- Do:
- if __name__ == '__main__':
- import cProfile
- cProfile.run('main()', 'myinfo.prof')
- ---
- # side note
- If you need to set up an environment, you can use `cProfile.runctx(command, globals, locals)`.
- ---
- # Need more detail!
- Then:
- #!/usr/bin/env python
-
- import pstats
-
- p = pstats.Stats('myinfo.prof')
- p.strip_dirs().sort_stats("time").print_stats(50)
- ---
- # pstats
- - `strip_dirs`
- - `sort_stats`: "calls", "cumulative", "pcalls", "time", etc
- - `print_stats`: restrictable (number of rows, regular expression)
- ---
- # pstats
- Also:
- - `print_callers`
- - `print_callees`
- - `reverse_order`
- - `dump_stats`
- ---
- # pstats
- Finally:
- - `add`
- ---
- # Whine whine whine
- It's only usable from the command line!
- ---
- # It's been wrapped
- See, for example, [repoze.profile](http://pypi.python.org/pypi/repoze.profile).
- ---
- # Questions?
- ---
- ---
|