blob: 627f2156ab9e952b0d13c0d1135a73524f2e5beb [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _profile:
2
3********************
4The Python Profilers
5********************
6
7.. sectionauthor:: James Roskind
8
Georg Brandl7232d412009-11-12 08:29:46 +00009.. module:: profile
10 :synopsis: Python source profiler.
Georg Brandl8ec7f652007-08-15 14:28:01 +000011
Éric Araujo29a0b572011-08-19 02:14:03 +020012**Source code:** :source:`Lib/profile.py` and :source:`Lib/pstats.py`
13
14--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
Georg Brandl8ec7f652007-08-15 14:28:01 +000016.. _profiler-introduction:
17
18Introduction to the profilers
19=============================
20
21.. index::
22 single: deterministic profiling
23 single: profiling, deterministic
24
Andrew M. Kuchling08923172008-04-18 18:39:55 +000025A :dfn:`profiler` is a program that describes the run time performance
26of a program, providing a variety of statistics. This documentation
27describes the profiler functionality provided in the modules
28:mod:`cProfile`, :mod:`profile` and :mod:`pstats`. This profiler
29provides :dfn:`deterministic profiling` of Python programs. It also
30provides a series of report generation tools to allow users to rapidly
Georg Brandl8ec7f652007-08-15 14:28:01 +000031examine the results of a profile operation.
32
33The Python standard library provides three different profilers:
34
Georg Brandlc62ef8b2009-01-03 20:55:06 +000035#. :mod:`cProfile` is recommended for most users; it's a C extension
Andrew M. Kuchling08923172008-04-18 18:39:55 +000036 with reasonable overhead
Georg Brandlc62ef8b2009-01-03 20:55:06 +000037 that makes it suitable for profiling long-running programs.
Andrew M. Kuchling08923172008-04-18 18:39:55 +000038 Based on :mod:`lsprof`,
Georg Brandlc62ef8b2009-01-03 20:55:06 +000039 contributed by Brett Rosen and Ted Czotter.
Georg Brandl8ec7f652007-08-15 14:28:01 +000040
41 .. versionadded:: 2.5
42
Andrew M. Kuchling08923172008-04-18 18:39:55 +000043#. :mod:`profile`, a pure Python module whose interface is imitated by
Georg Brandlc62ef8b2009-01-03 20:55:06 +000044 :mod:`cProfile`. Adds significant overhead to profiled programs.
45 If you're trying to extend
Andrew M. Kuchling08923172008-04-18 18:39:55 +000046 the profiler in some way, the task might be easier with this module.
Andrew M. Kuchling08923172008-04-18 18:39:55 +000047
48 .. versionchanged:: 2.4
Andrew Svetlov8a9b4012012-10-31 21:54:45 +020049 Now also reports the time spent in calls to built-in functions
50 and methods.
Andrew M. Kuchling08923172008-04-18 18:39:55 +000051
52#. :mod:`hotshot` was an experimental C module that focused on minimizing
53 the overhead of profiling, at the expense of longer data
54 post-processing times. It is no longer maintained and may be
55 dropped in a future version of Python.
Georg Brandlc62ef8b2009-01-03 20:55:06 +000056
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
58 .. versionchanged:: 2.5
Andrew M. Kuchling08923172008-04-18 18:39:55 +000059 The results should be more meaningful than in the past: the timing core
Georg Brandl8ec7f652007-08-15 14:28:01 +000060 contained a critical bug.
61
62The :mod:`profile` and :mod:`cProfile` modules export the same interface, so
Andrew M. Kuchling08923172008-04-18 18:39:55 +000063they are mostly interchangeable; :mod:`cProfile` has a much lower overhead but
64is newer and might not be available on all systems.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065:mod:`cProfile` is really a compatibility layer on top of the internal
Andrew M. Kuchling08923172008-04-18 18:39:55 +000066:mod:`_lsprof` module. The :mod:`hotshot` module is reserved for specialized
67usage.
Georg Brandl8ec7f652007-08-15 14:28:01 +000068
69
70.. _profile-instant:
71
72Instant User's Manual
73=====================
74
75This section is provided for users that "don't want to read the manual." It
76provides a very brief overview, and allows a user to rapidly perform profiling
77on an existing application.
78
79To profile an application with a main entry point of :func:`foo`, you would add
80the following to your module::
81
82 import cProfile
83 cProfile.run('foo()')
84
85(Use :mod:`profile` instead of :mod:`cProfile` if the latter is not available on
86your system.)
87
88The above action would cause :func:`foo` to be run, and a series of informative
89lines (the profile) to be printed. The above approach is most useful when
90working with the interpreter. If you would like to save the results of a
91profile into a file for later examination, you can supply a file name as the
92second argument to the :func:`run` function::
93
94 import cProfile
95 cProfile.run('foo()', 'fooprof')
96
97The file :file:`cProfile.py` can also be invoked as a script to profile another
98script. For example::
99
100 python -m cProfile myscript.py
101
102:file:`cProfile.py` accepts two optional arguments on the command line::
103
104 cProfile.py [-o output_file] [-s sort_order]
105
Georg Brandlc3b0cd72010-01-23 08:47:54 +0000106``-s`` only applies to standard output (``-o`` is not supplied).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107Look in the :class:`Stats` documentation for valid sort values.
108
109When you wish to review the profile, you should use the methods in the
110:mod:`pstats` module. Typically you would load the statistics data as follows::
111
112 import pstats
113 p = pstats.Stats('fooprof')
114
115The class :class:`Stats` (the above code just created an instance of this class)
116has a variety of methods for manipulating and printing the data that was just
117read into ``p``. When you ran :func:`cProfile.run` above, what was printed was
118the result of three method calls::
119
120 p.strip_dirs().sort_stats(-1).print_stats()
121
122The first method removed the extraneous path from all the module names. The
123second method sorted all the entries according to the standard module/line/name
124string that is printed. The third method printed out all the statistics. You
125might try the following sort calls:
126
Georg Brandlb19be572007-12-29 10:57:00 +0000127.. (this is to comply with the semantics of the old profiler).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000128
129::
130
131 p.sort_stats('name')
132 p.print_stats()
133
134The first call will actually sort the list by function name, and the second call
135will print out the statistics. The following are some interesting calls to
136experiment with::
137
138 p.sort_stats('cumulative').print_stats(10)
139
140This sorts the profile by cumulative time in a function, and then only prints
141the ten most significant lines. If you want to understand what algorithms are
142taking time, the above line is what you would use.
143
144If you were looking to see what functions were looping a lot, and taking a lot
145of time, you would do::
146
147 p.sort_stats('time').print_stats(10)
148
149to sort according to time spent within each function, and then print the
150statistics for the top ten functions.
151
152You might also try::
153
154 p.sort_stats('file').print_stats('__init__')
155
156This will sort all the statistics by file name, and then print out statistics
157for only the class init methods (since they are spelled with ``__init__`` in
158them). As one final example, you could try::
159
160 p.sort_stats('time', 'cum').print_stats(.5, 'init')
161
162This line sorts statistics with a primary key of time, and a secondary key of
163cumulative time, and then prints out some of the statistics. To be specific, the
164list is first culled down to 50% (re: ``.5``) of its original size, then only
165lines containing ``init`` are maintained, and that sub-sub-list is printed.
166
167If you wondered what functions called the above functions, you could now (``p``
168is still sorted according to the last criteria) do::
169
170 p.print_callers(.5, 'init')
171
172and you would get a list of callers for each of the listed functions.
173
174If you want more functionality, you're going to have to read the manual, or
175guess what the following functions do::
176
177 p.print_callees()
178 p.add('fooprof')
179
180Invoked as a script, the :mod:`pstats` module is a statistics browser for
181reading and examining profile dumps. It has a simple line-oriented interface
182(implemented using :mod:`cmd`) and interactive help.
183
184
185.. _deterministic-profiling:
186
187What Is Deterministic Profiling?
188================================
189
190:dfn:`Deterministic profiling` is meant to reflect the fact that all *function
191call*, *function return*, and *exception* events are monitored, and precise
192timings are made for the intervals between these events (during which time the
193user's code is executing). In contrast, :dfn:`statistical profiling` (which is
194not done by this module) randomly samples the effective instruction pointer, and
195deduces where time is being spent. The latter technique traditionally involves
196less overhead (as the code does not need to be instrumented), but provides only
197relative indications of where time is being spent.
198
199In Python, since there is an interpreter active during execution, the presence
200of instrumented code is not required to do deterministic profiling. Python
201automatically provides a :dfn:`hook` (optional callback) for each event. In
202addition, the interpreted nature of Python tends to add so much overhead to
203execution, that deterministic profiling tends to only add small processing
204overhead in typical applications. The result is that deterministic profiling is
205not that expensive, yet provides extensive run time statistics about the
206execution of a Python program.
207
208Call count statistics can be used to identify bugs in code (surprising counts),
209and to identify possible inline-expansion points (high call counts). Internal
210time statistics can be used to identify "hot loops" that should be carefully
211optimized. Cumulative time statistics should be used to identify high level
212errors in the selection of algorithms. Note that the unusual handling of
213cumulative times in this profiler allows statistics for recursive
214implementations of algorithms to be directly compared to iterative
215implementations.
216
217
218Reference Manual -- :mod:`profile` and :mod:`cProfile`
219======================================================
220
221.. module:: cProfile
222 :synopsis: Python profiler
223
224
225The primary entry point for the profiler is the global function
226:func:`profile.run` (resp. :func:`cProfile.run`). It is typically used to create
227any profile information. The reports are formatted and printed using methods of
228the class :class:`pstats.Stats`. The following is a description of all of these
229standard entry points and functions. For a more in-depth view of some of the
230code, consider reading the later section on Profiler Extensions, which includes
231discussion of how to derive "better" profilers from the classes presented, or
232reading the source code for these modules.
233
234
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200235.. function:: run(command[, filename][, sort])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236
237 This function takes a single argument that can be passed to the
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200238 :keyword:`exec` statement, and optionally a file name and a sorting
239 directive. In all cases this routine attempts to :keyword:`exec`
240 its first argument, and gather profiling statistics from the
241 execution. If no file name is present, then this function
242 automatically prints a simple profiling report, sorted by the
243 standard name string (file/line/function-name) that is presented in
244 each line. The following is a typical output from such a call::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000245
246 2706 function calls (2004 primitive calls) in 4.504 CPU seconds
247
248 Ordered by: standard name
249
250 ncalls tottime percall cumtime percall filename:lineno(function)
251 2 0.006 0.003 0.953 0.477 pobject.py:75(save_objects)
252 43/3 0.533 0.012 0.749 0.250 pobject.py:99(evaluate)
253 ...
254
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200255 The first line indicates that 2706 calls were monitored. Of those
256 calls, 2004 were :dfn:`primitive`. We define :dfn:`primitive` to
257 mean that the call was not induced via recursion. The next line:
258 ``Ordered by: standard name``, indicates that the text string in
259 the far right column was used to sort the output. The column
260 headings include:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000261
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000262 ncalls
Georg Brandl8ec7f652007-08-15 14:28:01 +0000263 for the number of calls,
264
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000265 tottime
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200266 for the total time spent in the given function (and
267 excluding time made in calls to sub-functions),
Georg Brandl8ec7f652007-08-15 14:28:01 +0000268
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000269 percall
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270 is the quotient of ``tottime`` divided by ``ncalls``
271
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000272 cumtime
Georg Brandl8ec7f652007-08-15 14:28:01 +0000273 is the total time spent in this and all subfunctions (from invocation till
274 exit). This figure is accurate *even* for recursive functions.
275
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000276 percall
Georg Brandl8ec7f652007-08-15 14:28:01 +0000277 is the quotient of ``cumtime`` divided by primitive calls
278
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000279 filename:lineno(function)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000280 provides the respective data of each function
281
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200282 When there are two numbers in the first column (for example,
283 ``43/3``), then the latter is the number of primitive calls, and
284 the former is the actual number of calls. Note that when the
285 function does not recurse, these two values are the same, and only
286 the single figure is printed. For information on the sort
287 argument, refer to :meth:`pstats.Stats.sort_stats`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000288
289
290.. function:: runctx(command, globals, locals[, filename])
291
292 This function is similar to :func:`run`, with added arguments to supply the
293 globals and locals dictionaries for the *command* string.
294
295Analysis of the profiler data is done using the :class:`Stats` class.
296
297.. note::
298
299 The :class:`Stats` class is defined in the :mod:`pstats` module.
300
301
302.. module:: pstats
303 :synopsis: Statistics object for use with the profiler.
304
305
Hynek Schlawacke58ce012012-05-22 10:27:40 +0200306.. class:: Stats(filename, stream=sys.stdout[, ...])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000307
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200308 This class constructor creates an instance of a "statistics object"
309 from a *filename* (or set of filenames). :class:`Stats` objects
310 are manipulated by methods, in order to print useful reports. You
311 may specify an alternate output stream by giving the keyword
312 argument, ``stream``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000313
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200314 The file selected by the above constructor must have been created
315 by the corresponding version of :mod:`profile` or :mod:`cProfile`.
316 To be specific, there is *no* file compatibility guaranteed with
317 future versions of this profiler, and there is no compatibility
318 with files produced by other profilers. If several files are
319 provided, all the statistics for identical functions will be
320 coalesced, so that an overall view of several processes can be
321 considered in a single report. If additional files need to be
322 combined with data in an existing :class:`Stats` object, the
323 :meth:`add` method can be used.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000324
Georg Brandlb19be572007-12-29 10:57:00 +0000325 .. (such as the old system profiler).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000326
327 .. versionchanged:: 2.5
328 The *stream* parameter was added.
329
330
331.. _profile-stats:
332
333The :class:`Stats` Class
334------------------------
335
336:class:`Stats` objects have the following methods:
337
338
339.. method:: Stats.strip_dirs()
340
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200341 This method for the :class:`Stats` class removes all leading path
342 information from file names. It is very useful in reducing the
343 size of the printout to fit within (close to) 80 columns. This
344 method modifies the object, and the stripped information is lost.
345 After performing a strip operation, the object is considered to
346 have its entries in a "random" order, as it was just after object
347 initialization and loading. If :meth:`strip_dirs` causes two
348 function names to be indistinguishable (they are on the same line
349 of the same filename, and have the same function name), then the
350 statistics for these two entries are accumulated into a single
351 entry.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000352
353
354.. method:: Stats.add(filename[, ...])
355
356 This method of the :class:`Stats` class accumulates additional profiling
357 information into the current profiling object. Its arguments should refer to
358 filenames created by the corresponding version of :func:`profile.run` or
359 :func:`cProfile.run`. Statistics for identically named (re: file, line, name)
360 functions are automatically accumulated into single function statistics.
361
362
363.. method:: Stats.dump_stats(filename)
364
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200365 Save the data loaded into the :class:`Stats` object to a file named
366 *filename*. The file is created if it does not exist, and is
367 overwritten if it already exists. This is equivalent to the method
368 of the same name on the :class:`profile.Profile` and
369 :class:`cProfile.Profile` classes.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000370
371 .. versionadded:: 2.3
372
373
374.. method:: Stats.sort_stats(key[, ...])
375
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200376 This method modifies the :class:`Stats` object by sorting it
377 according to the supplied criteria. The argument is typically a
378 string identifying the basis of a sort (example: ``'time'`` or
379 ``'name'``).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000380
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200381 When more than one key is provided, then additional keys are used
382 as secondary criteria when there is equality in all keys selected
383 before them. For example, ``sort_stats('name', 'file')`` will sort
384 all the entries according to their function name, and resolve all
385 ties (identical function names) by sorting by file name.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000386
387 Abbreviations can be used for any key names, as long as the abbreviation is
388 unambiguous. The following are the keys currently defined:
389
390 +------------------+----------------------+
391 | Valid Arg | Meaning |
392 +==================+======================+
393 | ``'calls'`` | call count |
394 +------------------+----------------------+
395 | ``'cumulative'`` | cumulative time |
396 +------------------+----------------------+
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200397 | ``'cumtime'`` | cumulative time |
398 +------------------+----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000399 | ``'file'`` | file name |
400 +------------------+----------------------+
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200401 | ``'filename'`` | file name |
402 +------------------+----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000403 | ``'module'`` | file name |
404 +------------------+----------------------+
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200405 | ``'ncalls'`` | call count |
406 +------------------+----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000407 | ``'pcalls'`` | primitive call count |
408 +------------------+----------------------+
409 | ``'line'`` | line number |
410 +------------------+----------------------+
411 | ``'name'`` | function name |
412 +------------------+----------------------+
413 | ``'nfl'`` | name/file/line |
414 +------------------+----------------------+
415 | ``'stdname'`` | standard name |
416 +------------------+----------------------+
417 | ``'time'`` | internal time |
418 +------------------+----------------------+
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200419 | ``'tottime'`` | internal time |
420 +------------------+----------------------+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000421
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200422 Note that all sorts on statistics are in descending order (placing
423 most time consuming items first), where as name, file, and line
424 number searches are in ascending order (alphabetical). The subtle
425 distinction between ``'nfl'`` and ``'stdname'`` is that the
426 standard name is a sort of the name as printed, which means that
427 the embedded line numbers get compared in an odd way. For example,
428 lines 3, 20, and 40 would (if the file names were the same) appear
429 in the string order 20, 3 and 40. In contrast, ``'nfl'`` does a
430 numeric compare of the line numbers. In fact,
431 ``sort_stats('nfl')`` is the same as ``sort_stats('name', 'file',
432 'line')``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000433
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200434 For backward-compatibility reasons, the numeric arguments ``-1``,
435 ``0``, ``1``, and ``2`` are permitted. They are interpreted as
436 ``'stdname'``, ``'calls'``, ``'time'``, and ``'cumulative'``
437 respectively. If this old style format (numeric) is used, only one
438 sort key (the numeric key) will be used, and additional arguments
439 will be silently ignored.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000440
Georg Brandlb19be572007-12-29 10:57:00 +0000441 .. For compatibility with the old profiler,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000442
443
444.. method:: Stats.reverse_order()
445
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200446 This method for the :class:`Stats` class reverses the ordering of
447 the basic list within the object. Note that by default ascending
448 vs descending order is properly selected based on the sort key of
449 choice.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000450
Georg Brandlb19be572007-12-29 10:57:00 +0000451 .. This method is provided primarily for compatibility with the old profiler.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000452
453
454.. method:: Stats.print_stats([restriction, ...])
455
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200456 This method for the :class:`Stats` class prints out a report as
457 described in the :func:`profile.run` definition.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000458
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200459 The order of the printing is based on the last :meth:`sort_stats`
460 operation done on the object (subject to caveats in :meth:`add` and
461 :meth:`strip_dirs`).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000462
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200463 The arguments provided (if any) can be used to limit the list down
464 to the significant entries. Initially, the list is taken to be the
465 complete set of profiled functions. Each restriction is either an
466 integer (to select a count of lines), or a decimal fraction between
467 0.0 and 1.0 inclusive (to select a percentage of lines), or a
468 regular expression (to pattern match the standard name that is
469 printed; as of Python 1.5b1, this uses the Perl-style regular
470 expression syntax defined by the :mod:`re` module). If several
471 restrictions are provided, then they are applied sequentially. For
472 example::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000473
474 print_stats(.1, 'foo:')
475
476 would first limit the printing to first 10% of list, and then only print
477 functions that were part of filename :file:`.\*foo:`. In contrast, the
478 command::
479
480 print_stats('foo:', .1)
481
482 would limit the list to all functions having file names :file:`.\*foo:`, and
483 then proceed to only print the first 10% of them.
484
485
486.. method:: Stats.print_callers([restriction, ...])
487
488 This method for the :class:`Stats` class prints a list of all functions that
489 called each function in the profiled database. The ordering is identical to
490 that provided by :meth:`print_stats`, and the definition of the restricting
491 argument is also identical. Each caller is reported on its own line. The
492 format differs slightly depending on the profiler that produced the stats:
493
494 * With :mod:`profile`, a number is shown in parentheses after each caller to
495 show how many times this specific call was made. For convenience, a second
496 non-parenthesized number repeats the cumulative time spent in the function
497 at the right.
498
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200499 * With :mod:`cProfile`, each caller is preceded by three numbers:
500 the number of times this specific call was made, and the total
501 and cumulative times spent in the current function while it was
502 invoked by this specific caller.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000503
504
505.. method:: Stats.print_callees([restriction, ...])
506
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200507 This method for the :class:`Stats` class prints a list of all
508 function that were called by the indicated function. Aside from
509 this reversal of direction of calls (re: called vs was called by),
510 the arguments and ordering are identical to the
511 :meth:`print_callers` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000512
513
514.. _profile-limits:
515
516Limitations
517===========
518
519One limitation has to do with accuracy of timing information. There is a
520fundamental problem with deterministic profilers involving accuracy. The most
521obvious restriction is that the underlying "clock" is only ticking at a rate
522(typically) of about .001 seconds. Hence no measurements will be more accurate
523than the underlying clock. If enough measurements are taken, then the "error"
524will tend to average out. Unfortunately, removing this first error induces a
525second source of error.
526
527The second problem is that it "takes a while" from when an event is dispatched
528until the profiler's call to get the time actually *gets* the state of the
529clock. Similarly, there is a certain lag when exiting the profiler event
530handler from the time that the clock's value was obtained (and then squirreled
531away), until the user's code is once again executing. As a result, functions
532that are called many times, or call many functions, will typically accumulate
533this error. The error that accumulates in this fashion is typically less than
534the accuracy of the clock (less than one clock tick), but it *can* accumulate
535and become very significant.
536
537The problem is more important with :mod:`profile` than with the lower-overhead
538:mod:`cProfile`. For this reason, :mod:`profile` provides a means of
539calibrating itself for a given platform so that this error can be
540probabilistically (on the average) removed. After the profiler is calibrated, it
541will be more accurate (in a least square sense), but it will sometimes produce
542negative numbers (when call counts are exceptionally low, and the gods of
543probability work against you :-). ) Do *not* be alarmed by negative numbers in
544the profile. They should *only* appear if you have calibrated your profiler,
545and the results are actually better than without calibration.
546
547
548.. _profile-calibration:
549
550Calibration
551===========
552
553The profiler of the :mod:`profile` module subtracts a constant from each event
554handling time to compensate for the overhead of calling the time function, and
555socking away the results. By default, the constant is 0. The following
556procedure can be used to obtain a better constant for a given platform (see
557discussion in section Limitations above). ::
558
559 import profile
560 pr = profile.Profile()
561 for i in range(5):
562 print pr.calibrate(10000)
563
564The method executes the number of Python calls given by the argument, directly
565and again under the profiler, measuring the time for both. It then computes the
566hidden overhead per profiler event, and returns that as a float. For example,
567on an 800 MHz Pentium running Windows 2000, and using Python's time.clock() as
568the timer, the magical number is about 12.5e-6.
569
570The object of this exercise is to get a fairly consistent result. If your
571computer is *very* fast, or your timer function has poor resolution, you might
572have to pass 100000, or even 1000000, to get consistent results.
573
574When you have a consistent answer, there are three ways you can use it: [#]_ ::
575
576 import profile
577
578 # 1. Apply computed bias to all Profile instances created hereafter.
579 profile.Profile.bias = your_computed_bias
580
581 # 2. Apply computed bias to a specific Profile instance.
582 pr = profile.Profile()
583 pr.bias = your_computed_bias
584
585 # 3. Specify computed bias in instance constructor.
586 pr = profile.Profile(bias=your_computed_bias)
587
588If you have a choice, you are better off choosing a smaller constant, and then
589your results will "less often" show up as negative in profile statistics.
590
591
592.. _profiler-extensions:
593
594Extensions --- Deriving Better Profilers
595========================================
596
597The :class:`Profile` class of both modules, :mod:`profile` and :mod:`cProfile`,
598were written so that derived classes could be developed to extend the profiler.
599The details are not described here, as doing this successfully requires an
600expert understanding of how the :class:`Profile` class works internally. Study
601the source code of the module carefully if you want to pursue this.
602
603If all you want to do is change how current time is determined (for example, to
604force use of wall-clock time or elapsed process time), pass the timing function
605you want to the :class:`Profile` class constructor::
606
607 pr = profile.Profile(your_time_func)
608
609The resulting profiler will then call :func:`your_time_func`.
610
611:class:`profile.Profile`
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200612 :func:`your_time_func` should return a single number, or a list of
613 numbers whose sum is the current time (like what :func:`os.times`
614 returns). If the function returns a single time number, or the
615 list of returned numbers has length 2, then you will get an
616 especially fast version of the dispatch routine.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000617
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200618 Be warned that you should calibrate the profiler class for the
619 timer function that you choose. For most machines, a timer that
620 returns a lone integer value will provide the best results in terms
621 of low overhead during profiling. (:func:`os.times` is *pretty*
622 bad, as it returns a tuple of floating point values). If you want
623 to substitute a better timer in the cleanest fashion, derive a
624 class and hardwire a replacement dispatch method that best handles
625 your timer call, along with the appropriate calibration constant.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000626
627:class:`cProfile.Profile`
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200628 :func:`your_time_func` should return a single number. If it
629 returns plain integers, you can also invoke the class constructor
630 with a second argument specifying the real duration of one unit of
631 time. For example, if :func:`your_integer_time_func` returns times
632 measured in thousands of seconds, you would construct the
633 :class:`Profile` instance as follows::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000634
635 pr = profile.Profile(your_integer_time_func, 0.001)
636
Andrew Svetlov8a9b4012012-10-31 21:54:45 +0200637 As the :mod:`cProfile.Profile` class cannot be calibrated, custom
638 timer functions should be used with care and should be as fast as
639 possible. For the best results with a custom timer, it might be
640 necessary to hard-code it in the C source of the internal
641 :mod:`_lsprof` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000642
Georg Brandl10146862012-07-01 09:40:16 +0200643
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644.. rubric:: Footnotes
645
Georg Brandl10146862012-07-01 09:40:16 +0200646.. [#] Prior to Python 2.2, it was necessary to edit the profiler source code to
647 embed the bias as a literal number. You still can, but that method is no longer
Georg Brandl8ec7f652007-08-15 14:28:01 +0000648 described, because no longer needed.