home *** CD-ROM | disk | FTP | other *** search
- <TITLE>Instant Users Manual -- Python library reference</TITLE>
- Next: <A HREF="../d/deterministic_profiling" TYPE="Next">Deterministic Profiling</A>
- Prev: <A HREF="../p/profiler_changes" TYPE="Prev">Profiler Changes</A>
- Up: <A HREF="../t/the_python_profiler" TYPE="Up">The Python Profiler</A>
- Top: <A HREF="../t/top" TYPE="Top">Top</A>
- <H1>9.3. Instant Users Manual</H1>
- This section is provided for users that ``don't want to read the
- manual.'' It provides a very brief overview, and allows a user to
- rapidly perform profiling on an existing application.
- <P>
- To profile an application with a main entry point of `<SAMP>foo()</SAMP>', you
- would add the following to your module:
- <P>
- <UL COMPACT><CODE> import profile<P>
- profile.run("foo()")<P>
- </CODE></UL>
- The above action would cause `<SAMP>foo()</SAMP>' to be run, and a series of
- informative lines (the profile) to be printed. The above approach is
- most useful when working with the interpreter. If you would like to
- save the results of a profile into a file for later examination, you
- can supply a file name as the second argument to the <CODE>run()</CODE>
- function:
- <P>
- <UL COMPACT><CODE> import profile<P>
- profile.run("foo()", 'fooprof')<P>
- </CODE></UL>
- When you wish to review the profile, you should use the methods in the
- <CODE>pstats</CODE> module. Typically you would load the statistics data as
- follows:
- <P>
- <UL COMPACT><CODE> import pstats<P>
- p = pstats.Stats('fooprof')<P>
- </CODE></UL>
- The class <CODE>Stats</CODE> (the above code just created an instance of
- this class) has a variety of methods for manipulating and printing the
- data that was just read into `<SAMP>p</SAMP>'. When you ran
- <CODE>profile.run()</CODE> above, what was printed was the result of three
- method calls:
- <P>
- <UL COMPACT><CODE> p.strip_dirs().sort_stats(-1).print_stats()<P>
- </CODE></UL>
- The first method removed the extraneous path from all the module
- names. The second method sorted all the entries according to the
- standard module/line/name string that is printed (this is to comply
- with the semantics of the old profiler). The third method printed out
- all the statistics. You might try the following sort calls:
- <P>
- <UL COMPACT><CODE> p.sort_stats('name')<P>
- p.print_stats()<P>
- </CODE></UL>
- The first call will actually sort the list by function name, and the
- second call will print out the statistics. The following are some
- interesting calls to experiment with:
- <P>
- <UL COMPACT><CODE> p.sort_stats('cumulative').print_stats(10)<P>
- </CODE></UL>
- This sorts the profile by cumulative time in a function, and then only
- prints the ten most significant lines. If you want to understand what
- algorithms are taking time, the above line is what you would use.
- <P>
- If you were looking to see what functions were looping a lot, and
- taking a lot of time, you would do:
- <P>
- <UL COMPACT><CODE> p.sort_stats('time').print_stats(10)<P>
- </CODE></UL>
- to sort according to time spent within each function, and then print
- the statistics for the top ten functions.
- <P>
- You might also try:
- <P>
- <UL COMPACT><CODE> p.sort_stats('file').print_stats('__init__')<P>
- </CODE></UL>
- This will sort all the statistics by file name, and then print out
- statistics for only the class init methods ('cause they are spelled
- with <CODE>__init__</CODE> in them). As one final example, you could try:
- <P>
- <UL COMPACT><CODE> p.sort_stats('time', 'cum').print_stats(.5, 'init')<P>
- </CODE></UL>
- This line sorts statistics with a primary key of time, and a secondary
- key of cumulative time, and then prints out some of the statistics.
- To be specific, the list is first culled down to 50% (re: `<SAMP>.5</SAMP>')
- of its original size, then only lines containing <CODE>init</CODE> are
- maintained, and that sub-sub-list is printed.
- <P>
- If you wondered what functions called the above functions, you could
- now (`<SAMP>p</SAMP>' is still sorted according to the last criteria) do:
- <P>
- <UL COMPACT><CODE> p.print_callers(.5, 'init')<P>
- </CODE></UL>
- and you would get a list of callers for each of the listed functions.
- <P>
- If you want more functionality, you're going to have to read the
- manual, or guess what the following functions do:
- <P>
- <UL COMPACT><CODE> p.print_callees()<P>
- p.add('fooprof')<P>
- </CODE></UL>
-