profile/cProfile

John Melesky (PDX Python, May 2010)

In the standard library

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

pstats

Also:

pstats

Finally:

Whine whine whine

It's only usable from the command line!

It's been wrapped

See, for example, repoze.profile.

Questions?