1
- \chapter {The Python Profiler \label {profile } }
1
+ \chapter {The Python Profilers \label {profile } }
2
2
3
3
\sectionauthor {James Roskind}{}
4
4
5
5
Copyright \copyright {} 1994, by InfoSeek Corporation, all rights reserved.
6
6
\index {InfoSeek Corporation}
7
7
8
8
Written by James Roskind.\footnote {
9
- Updated and converted to \LaTeX \ by Guido van Rossum. The references to
10
- the old profiler are left in the text, although it no longer exists.}
9
+ Updated and converted to \LaTeX \ by Guido van Rossum.
10
+ Further updated by Armin Rigo to integrate the documentation for the new
11
+ \module {cProfile} module of Python 2.5.}
11
12
12
13
Permission to use, copy, modify, and distribute this Python software
13
14
and its associated documentation for any purpose (subject to the
@@ -41,7 +42,7 @@ \chapter{The Python Profiler \label{profile}}
41
42
I'd appreciate the feedback.
42
43
43
44
44
- \section {Introduction to the profiler }
45
+ \section {Introduction to the profilers }
45
46
\nodename {Profiler Introduction}
46
47
47
48
A \dfn {profiler} is a program that describes the run time performance
@@ -54,6 +55,31 @@ \section{Introduction to the profiler}
54
55
\index {deterministic profiling}
55
56
\index {profiling, deterministic}
56
57
58
+ The Python standard library provides three different profilers:
59
+
60
+ \begin {enumerate }
61
+ \item \module {profile}, a pure Python module, described in the sequel.
62
+ Copyright \copyright {} 1994, by InfoSeek Corporation.
63
+ \versionchanged [also reports the time spent in calls to built-in
64
+ functions and methods]{2.4}
65
+
66
+ \item \module {cProfile}, a module written in C, with a reasonable
67
+ overhead that makes it suitable for profiling long-running programs.
68
+ Based on \module {lsprof}, contributed by Brett Rosen and Ted Czotter.
69
+ \versionadded {2.5}
70
+
71
+ \item \module {hotshot}, a C module focusing on minimizing the overhead
72
+ while profiling, at the expense of long data post-processing times.
73
+ \versionchanged [the results should be more meaningful than in the
74
+ past: the timing core contained a critical bug]{2.5}
75
+ \end {enumerate }
76
+
77
+ The \module {profile} and \module {cProfile} modules export the same
78
+ interface, so they are mostly interchangeables; \module {cProfile} has a
79
+ much lower overhead but is not so far as well-tested and might not be
80
+ available on all systems. \module {cProfile} is really a compatibility
81
+ layer on top of the internal \module {_lsprof} module. The
82
+ \module {hotshot} module is reserved to specialized usages.
57
83
58
84
% \section{How Is This Profiler Different From The Old Profiler?}
59
85
% \nodename{Profiler Changes}
@@ -108,10 +134,13 @@ \section{Instant Users Manual \label{profile-instant}}
108
134
you would add the following to your module:
109
135
110
136
\begin {verbatim }
111
- import profile
112
- profile .run('foo()')
137
+ import cProfile
138
+ cProfile .run('foo()')
113
139
\end {verbatim }
114
140
141
+ (Use \module {profile} instead of \module {cProfile} if the latter is not
142
+ available on your system.)
143
+
115
144
The above action would cause \function {foo()} to be run, and a series of
116
145
informative lines (the profile) to be printed. The above approach is
117
146
most useful when working with the interpreter. If you would like to
@@ -120,21 +149,21 @@ \section{Instant Users Manual \label{profile-instant}}
120
149
function:
121
150
122
151
\begin {verbatim }
123
- import profile
124
- profile .run('foo()', 'fooprof')
152
+ import cProfile
153
+ cProfile .run('foo()', 'fooprof')
125
154
\end {verbatim }
126
155
127
- The file \file {profile .py} can also be invoked as
156
+ The file \file {cProfile .py} can also be invoked as
128
157
a script to profile another script. For example:
129
158
130
159
\begin {verbatim }
131
- python -m profile myscript.py
160
+ python -m cProfile myscript.py
132
161
\end {verbatim }
133
162
134
- \file {profile .py} accepts two optional arguments on the command line:
163
+ \file {cProfile .py} accepts two optional arguments on the command line:
135
164
136
165
\begin {verbatim }
137
- profile .py [-o output_file] [-s sort_order]
166
+ cProfile .py [-o output_file] [-s sort_order]
138
167
\end {verbatim }
139
168
140
169
\programopt {-s} only applies to standard output (\programopt {-o} is
@@ -153,7 +182,7 @@ \section{Instant Users Manual \label{profile-instant}}
153
182
The class \class {Stats} (the above code just created an instance of
154
183
this class) has a variety of methods for manipulating and printing the
155
184
data that was just read into \code {p}. When you ran
156
- \function {profile .run()} above, what was printed was the result of three
185
+ \function {cProfile .run()} above, what was printed was the result of three
157
186
method calls:
158
187
159
188
\begin {verbatim }
@@ -162,8 +191,9 @@ \section{Instant Users Manual \label{profile-instant}}
162
191
163
192
The first method removed the extraneous path from all the module
164
193
names. The second method sorted all the entries according to the
165
- standard module/line/name string that is printed (this is to comply
166
- with the semantics of the old profiler). The third method printed out
194
+ standard module/line/name string that is printed.
195
+ % (this is to comply with the semantics of the old profiler).
196
+ The third method printed out
167
197
all the statistics. You might try the following sort calls:
168
198
169
199
\begin {verbatim }
@@ -268,15 +298,17 @@ \section{What Is Deterministic Profiling?}
268
298
of algorithms to be directly compared to iterative implementations.
269
299
270
300
271
- \section {Reference Manual }
301
+ \section {Reference Manual -- \module {profile} and \module {cProfile} }
272
302
273
303
\declaremodule {standard}{profile}
304
+ \declaremodule {standard}{cProfile}
274
305
\modulesynopsis {Python profiler}
275
306
276
307
277
308
278
309
The primary entry point for the profiler is the global function
279
- \function {profile.run()}. It is typically used to create any profile
310
+ \function {profile.run()} (resp. \function {cProfile.run()}).
311
+ It is typically used to create any profile
280
312
information. The reports are formatted and printed using methods of
281
313
the class \class {pstats.Stats}. The following is a description of all
282
314
of these standard entry points and functions. For a more in-depth
@@ -296,7 +328,6 @@ \section{Reference Manual}
296
328
each line. The following is a typical output from such a call:
297
329
298
330
\begin {verbatim }
299
- main()
300
331
2706 function calls (2004 primitive calls) in 4.504 CPU seconds
301
332
302
333
Ordered by: standard name
@@ -307,9 +338,7 @@ \section{Reference Manual}
307
338
...
308
339
\end {verbatim }
309
340
310
- The first line indicates that this profile was generated by the call:\\
311
- \code {profile.run('main()')}, and hence the exec'ed string is
312
- \code {'main()'}. The second line indicates that 2706 calls were
341
+ The first line indicates that 2706 calls were
313
342
monitored. Of those calls, 2004 were \dfn {primitive}. We define
314
343
\dfn {primitive} to mean that the call was not induced via recursion.
315
344
The next line: \code {Ordered by:\ standard name}, indicates that
@@ -350,7 +379,7 @@ \section{Reference Manual}
350
379
\end {funcdesc }
351
380
352
381
\begin {funcdesc }{runctx}{command, globals, locals\optional {, filename}}
353
- This function is similar to \function {profile. run()}, with added
382
+ This function is similar to \function {run()}, with added
354
383
arguments to supply the globals and locals dictionaries for the
355
384
\var {command} string.
356
385
\end {funcdesc }
@@ -368,10 +397,12 @@ \section{Reference Manual}
368
397
manipulated by methods, in order to print useful reports.
369
398
370
399
The file selected by the above constructor must have been created by
371
- the corresponding version of \module {profile}. To be specific, there is
400
+ the corresponding version of \module {profile} or \module {cProfile}.
401
+ To be specific, there is
372
402
\emph {no } file compatibility guaranteed with future versions of this
373
403
profiler, and there is no compatibility with files produced by other
374
- profilers (such as the old system profiler).
404
+ profilers.
405
+ % (such as the old system profiler).
375
406
376
407
If several files are provided, all the statistics for identical
377
408
functions will be coalesced, so that an overall view of several
@@ -403,7 +434,8 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
403
434
This method of the \class {Stats} class accumulates additional
404
435
profiling information into the current profiling object. Its
405
436
arguments should refer to filenames created by the corresponding
406
- version of \function {profile.run()}. Statistics for identically named
437
+ version of \function {profile.run()} or \function {cProfile.run()}.
438
+ Statistics for identically named
407
439
(re: file, line, name) functions are automatically accumulated into
408
440
single function statistics.
409
441
\end {methoddesc }
@@ -412,7 +444,8 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
412
444
Save the data loaded into the \class {Stats} object to a file named
413
445
\var {filename}. The file is created if it does not exist, and is
414
446
overwritten if it already exists. This is equivalent to the method of
415
- the same name on the \class {profile.Profile} class.
447
+ the same name on the \class {profile.Profile} and
448
+ \class {cProfile.Profile} classes.
416
449
\versionadded {2.3}
417
450
\end {methoddesc }
418
451
@@ -456,7 +489,8 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
456
489
compare of the line numbers. In fact, \code {sort_stats('nfl')} is the
457
490
same as \code {sort_stats('name', 'file' , 'line' )}.
458
491
459
- For compatibility with the old profiler, the numeric arguments
492
+ % For compatibility with the old profiler,
493
+ For backward-compatibility reasons, the numeric arguments
460
494
\code {-1}, \code {0}, \code {1}, and \code {2} are permitted. They are
461
495
interpreted as \code {'stdname'}, \code {'calls'}, \code {'time'}, and
462
496
\code {'cumulative'} respectively. If this old style format (numeric)
@@ -467,10 +501,10 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
467
501
468
502
\begin {methoddesc }[Stats]{reverse_order}{}
469
503
This method for the \class {Stats} class reverses the ordering of the basic
470
- list within the object. This method is provided primarily for
471
- compatibility with the old profiler. Its utility is questionable
472
- now that ascending vs descending order is properly selected based on
473
- the sort key of choice.
504
+ list within the object. % This method is provided primarily for
505
+ % compatibility with the old profiler.
506
+ Note that by default ascending vs descending order is properly selected
507
+ based on the sort key of choice.
474
508
\end {methoddesc }
475
509
476
510
\begin {methoddesc }[Stats]{print_stats}{\optional {restriction, \moreargs }}
@@ -512,10 +546,21 @@ \subsection{The \class{Stats} Class \label{profile-stats}}
512
546
This method for the \class {Stats} class prints a list of all functions
513
547
that called each function in the profiled database. The ordering is
514
548
identical to that provided by \method {print_stats()}, and the definition
515
- of the restricting argument is also identical. For convenience, a
516
- number is shown in parentheses after each caller to show how many
517
- times this specific call was made. A second non-parenthesized number
518
- is the cumulative time spent in the function at the right.
549
+ of the restricting argument is also identical. Each caller is reported on
550
+ its own line. The format differs slightly depending on the profiler that
551
+ produced the stats:
552
+
553
+ \begin {itemize }
554
+ \item With \module {profile}, a number is shown in parentheses after each
555
+ caller to show how many times this specific call was made. For
556
+ convenience, a second non-parenthesized number repeats the cumulative
557
+ time spent in the function at the right.
558
+
559
+ \item With \module {cProfile}, each caller is preceeded by three numbers:
560
+ the number of times this specific call was made, and the total and
561
+ cumulative times spent in the current function while it was invoked by
562
+ this specific caller.
563
+ \end {itemize }
519
564
\end {methoddesc }
520
565
521
566
\begin {methoddesc }[Stats]{print_callees}{\optional {restriction, \moreargs }}
@@ -546,7 +591,10 @@ \section{Limitations \label{profile-limits}}
546
591
times, or call many functions, will typically accumulate this error.
547
592
The error that accumulates in this fashion is typically less than the
548
593
accuracy of the clock (less than one clock tick), but it
549
- \emph {can } accumulate and become very significant. This profiler
594
+ \emph {can } accumulate and become very significant.
595
+
596
+ The problem is more important with \module {profile} than with the
597
+ lower-overhead \module {cProfile}. For this reason, \module {profile}
550
598
provides a means of calibrating itself for a given platform so that
551
599
this error can be probabilistically (on the average) removed.
552
600
After the profiler is calibrated, it will be more accurate (in a least
@@ -560,7 +608,7 @@ \section{Limitations \label{profile-limits}}
560
608
561
609
\section {Calibration \label {profile-calibration } }
562
610
563
- The profiler subtracts a constant from each
611
+ The profiler of the \module {profile} module subtracts a constant from each
564
612
event handling time to compensate for the overhead of calling the time
565
613
function, and socking away the results. By default, the constant is 0.
566
614
The following procedure can
@@ -614,11 +662,12 @@ \section{Calibration \label{profile-calibration}}
614
662
\section {Extensions --- Deriving Better Profilers }
615
663
\nodename {Profiler Extensions}
616
664
617
- The \class {Profile} class of module \module {profile} was written so that
665
+ The \class {Profile} class of both modules, \module {profile} and
666
+ \module {cProfile}, were written so that
618
667
derived classes could be developed to extend the profiler. The details
619
668
are not described here, as doing this successfully requires an expert
620
669
understanding of how the \class {Profile} class works internally. Study
621
- the source code of module \ module{profile} carefully if you want to
670
+ the source code of the module carefully if you want to
622
671
pursue this.
623
672
624
673
If all you want to do is change how current time is determined (for
@@ -630,8 +679,11 @@ \section{Extensions --- Deriving Better Profilers}
630
679
pr = profile.Profile(your_time_func)
631
680
\end {verbatim }
632
681
633
- The resulting profiler will then call \code {your_time_func()}.
634
- The function should return a single number, or a list of
682
+ The resulting profiler will then call \function {your_time_func()}.
683
+
684
+ \begin {description }
685
+ \item [\class {profile.Profile}]
686
+ \function {your_time_func()} should return a single number, or a list of
635
687
numbers whose sum is the current time (like what \function {os.times()}
636
688
returns). If the function returns a single time number, or the list of
637
689
returned numbers has length 2, then you will get an especially fast
@@ -646,3 +698,22 @@ \section{Extensions --- Deriving Better Profilers}
646
698
derive a class and hardwire a replacement dispatch method that best
647
699
handles your timer call, along with the appropriate calibration
648
700
constant.
701
+
702
+ \item [\class {cProfile.Profile}]
703
+ \function {your_time_func()} should return a single number. If it returns
704
+ plain integers, you can also invoke the class constructor with a second
705
+ argument specifying the real duration of one unit of time. For example,
706
+ if \function {your_integer_time_func()} returns times measured in thousands
707
+ of seconds, you would constuct the \class {Profile} instance as follows:
708
+
709
+ \begin {verbatim }
710
+ pr = profile.Profile(your_integer_time_func, 0.001)
711
+ \end {verbatim }
712
+
713
+ As the \module {cProfile.Profile} class cannot be calibrated, custom
714
+ timer functions should be used with care and should be as fast as
715
+ possible. For the best results with a custom timer, it might be
716
+ necessary to hard-code it in the C source of the internal
717
+ \module {_lsprof} module.
718
+
719
+ \end {description }
0 commit comments