Skip to content

Instantly share code, notes, and snippets.

@jebeck
Last active August 4, 2016 08:01
Show Gist options
  • Save jebeck/c3295ee4abb7c9f27582 to your computer and use it in GitHub Desktop.
Save jebeck/c3295ee4abb7c9f27582 to your computer and use it in GitHub Desktop.
profile a Python module with hotshot

Python Hotshot Profiler

Just a quick script to profile any Python module (filename passed in as a command-line arg; has to have a main()) using hotshot. Logs to stdout.

<!DOCTYPE html>
<html></html>
import hotshot, hotshot.stats
import sys
def main():
module = __import__(sys.argv[1].replace('.py', ''))
prof = hotshot.Profile('example.prof')
prof.runcall(module.main)
prof.close()
stats = hotshot.stats.load('example.prof')
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment