index.txt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # profile/cProfile
  2. John Melesky
  3. (PDX Python, May 2010)
  4. ---
  5. # In the standard library
  6. - hotshot
  7. - timeit
  8. - profile/cprofile/pstats
  9. ---
  10. # hotshot
  11. ---
  12. # timeit
  13. ---
  14. # profile/cProfile
  15. Instead of:
  16. if __name__ == '__main__':
  17. main()
  18. Do:
  19. if __name__ == '__main__':
  20. import cProfile
  21. cProfile.run('main()')
  22. ---
  23. # Questions?
  24. ---
  25. # Need more detail!
  26. ---
  27. # Need more detail!
  28. Instead of:
  29. if __name__ == '__main__':
  30. import cProfile
  31. cProfile.run('main()')
  32. Do:
  33. if __name__ == '__main__':
  34. import cProfile
  35. cProfile.run('main()', 'myinfo.prof')
  36. ---
  37. # side note
  38. If you need to set up an environment, you can use `cProfile.runctx(command, globals, locals)`.
  39. ---
  40. # Need more detail!
  41. Then:
  42. #!/usr/bin/env python
  43. import pstats
  44. p = pstats.Stats('myinfo.prof')
  45. p.strip_dirs().sort_stats("time").print_stats(50)
  46. ---
  47. # pstats
  48. - `strip_dirs`
  49. - `sort_stats`: "calls", "cumulative", "pcalls", "time", etc
  50. - `print_stats`: restrictable (number of rows, regular expression)
  51. ---
  52. # pstats
  53. Also:
  54. - `print_callers`
  55. - `print_callees`
  56. - `reverse_order`
  57. - `dump_stats`
  58. ---
  59. # pstats
  60. Finally:
  61. - `add`
  62. ---
  63. # Whine whine whine
  64. It's only usable from the command line!
  65. ---
  66. # It's been wrapped
  67. See, for example, [repoze.profile](http://pypi.python.org/pypi/repoze.profile).
  68. ---
  69. # Questions?
  70. ---
  71. ---