John Melesky (PDX Python, May 2010)
Instead of:
if __name__ == '__main__':
    main()
Do:
if __name__ == '__main__':
    import cProfile
    cProfile.run('main()')
Instead of:
if __name__ == '__main__':
    import cProfile
    cProfile.run('main()')
Do:
if __name__ == '__main__':
    import cProfile
    cProfile.run('main()', 'myinfo.prof')
If you need to set up an environment, you can use cProfile.runctx(command, globals, locals).
Then:
#!/usr/bin/env python
import pstats
p = pstats.Stats('myinfo.prof')
p.strip_dirs().sort_stats("time").print_stats(50)
strip_dirssort_stats: "calls", "cumulative", "pcalls", "time", etcprint_stats: restrictable (number of rows, regular expression)Also:
print_callersprint_calleesreverse_orderdump_statsFinally:
addIt's only usable from the command line!
See, for example, repoze.profile.