# 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!

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.

# Questions?