home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / i / instant_us < prev    next >
Text File  |  1996-11-14  |  4KB  |  98 lines

  1. <TITLE>Instant Users Manual -- Python library reference</TITLE>
  2. Next: <A HREF="../d/deterministic_profiling" TYPE="Next">Deterministic Profiling</A>  
  3. Prev: <A HREF="../p/profiler_changes" TYPE="Prev">Profiler Changes</A>  
  4. Up: <A HREF="../t/the_python_profiler" TYPE="Up">The Python Profiler</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H1>9.3. Instant Users Manual</H1>
  7. This section is provided for users that ``don't want to read the
  8. manual.'' It provides a very brief overview, and allows a user to
  9. rapidly perform profiling on an existing application.
  10. <P>
  11. To profile an application with a main entry point of `<SAMP>foo()</SAMP>', you
  12. would add the following to your module:
  13. <P>
  14. <UL COMPACT><CODE>    import profile<P>
  15.     profile.run("foo()")<P>
  16. </CODE></UL>
  17. The above action would cause `<SAMP>foo()</SAMP>' to be run, and a series of
  18. informative lines (the profile) to be printed.  The above approach is
  19. most useful when working with the interpreter.  If you would like to
  20. save the results of a profile into a file for later examination, you
  21. can supply a file name as the second argument to the <CODE>run()</CODE>
  22. function:
  23. <P>
  24. <UL COMPACT><CODE>    import profile<P>
  25.     profile.run("foo()", 'fooprof')<P>
  26. </CODE></UL>
  27. When you wish to review the profile, you should use the methods in the
  28. <CODE>pstats</CODE> module.  Typically you would load the statistics data as
  29. follows:
  30. <P>
  31. <UL COMPACT><CODE>    import pstats<P>
  32.     p = pstats.Stats('fooprof')<P>
  33. </CODE></UL>
  34. The class <CODE>Stats</CODE> (the above code just created an instance of
  35. this class) has a variety of methods for manipulating and printing the
  36. data that was just read into `<SAMP>p</SAMP>'.  When you ran
  37. <CODE>profile.run()</CODE> above, what was printed was the result of three
  38. method calls:
  39. <P>
  40. <UL COMPACT><CODE>    p.strip_dirs().sort_stats(-1).print_stats()<P>
  41. </CODE></UL>
  42. The first method removed the extraneous path from all the module
  43. names. The second method sorted all the entries according to the
  44. standard module/line/name string that is printed (this is to comply
  45. with the semantics of the old profiler).  The third method printed out
  46. all the statistics.  You might try the following sort calls:
  47. <P>
  48. <UL COMPACT><CODE>    p.sort_stats('name')<P>
  49.     p.print_stats()<P>
  50. </CODE></UL>
  51. The first call will actually sort the list by function name, and the
  52. second call will print out the statistics.  The following are some
  53. interesting calls to experiment with:
  54. <P>
  55. <UL COMPACT><CODE>    p.sort_stats('cumulative').print_stats(10)<P>
  56. </CODE></UL>
  57. This sorts the profile by cumulative time in a function, and then only
  58. prints the ten most significant lines.  If you want to understand what
  59. algorithms are taking time, the above line is what you would use.
  60. <P>
  61. If you were looking to see what functions were looping a lot, and
  62. taking a lot of time, you would do:
  63. <P>
  64. <UL COMPACT><CODE>    p.sort_stats('time').print_stats(10)<P>
  65. </CODE></UL>
  66. to sort according to time spent within each function, and then print
  67. the statistics for the top ten functions.
  68. <P>
  69. You might also try:
  70. <P>
  71. <UL COMPACT><CODE>    p.sort_stats('file').print_stats('__init__')<P>
  72. </CODE></UL>
  73. This will sort all the statistics by file name, and then print out
  74. statistics for only the class init methods ('cause they are spelled
  75. with <CODE>__init__</CODE> in them).  As one final example, you could try:
  76. <P>
  77. <UL COMPACT><CODE>    p.sort_stats('time', 'cum').print_stats(.5, 'init')<P>
  78. </CODE></UL>
  79. This line sorts statistics with a primary key of time, and a secondary
  80. key of cumulative time, and then prints out some of the statistics.
  81. To be specific, the list is first culled down to 50% (re: `<SAMP>.5</SAMP>')
  82. of its original size, then only lines containing <CODE>init</CODE> are
  83. maintained, and that sub-sub-list is printed.
  84. <P>
  85. If you wondered what functions called the above functions, you could
  86. now (`<SAMP>p</SAMP>' is still sorted according to the last criteria) do:
  87. <P>
  88. <UL COMPACT><CODE>    p.print_callers(.5, 'init')<P>
  89. </CODE></UL>
  90. and you would get a list of callers for each of the listed functions. 
  91. <P>
  92. If you want more functionality, you're going to have to read the
  93. manual, or guess what the following functions do:
  94. <P>
  95. <UL COMPACT><CODE>    p.print_callees()<P>
  96.     p.add('fooprof')<P>
  97. </CODE></UL>
  98.