Python/Profiling: Difference between revisions
From charlesreid1
(Created page with "=Introduction= ==List of Python Profiling Tools== Profiling tools provided by Python: * cProfile is a C extension, the recommended method of profiling python programs, and i...") |
|||
| Line 2: | Line 2: | ||
==List of Python Profiling Tools== | ==List of Python Profiling Tools== | ||
===Built Into Python=== | |||
Profiling tools provided by Python: | Profiling tools provided by Python: | ||
| Line 7: | Line 9: | ||
* <s>profile is a module that adds significant overhead; reports time spent in calls to built-in functions and methods</s> | * <s>profile is a module that adds significant overhead; reports time spent in calls to built-in functions and methods</s> | ||
* <s>hotshot - not developed/supported anymore</s> | * <s>hotshot - not developed/supported anymore</s> | ||
Benefits: | |||
* Easy | |||
* Built-in | |||
Downsides: | |||
* As stated by rkern [https://github.com/rkern/line_profiler], "The current profiling tools supported in Python 2.7 and later only time function calls. This is a good first step for locating hotspots in one's program and is frequently all one needs to do to optimize the program. However, sometimes the cause of the hotspot is actually a single line in the function, and that line may not be obvious from just reading the source code." | |||
Link: https://docs.python.org/2/library/profile.html | Link: https://docs.python.org/2/library/profile.html | ||
===Line by Line=== | |||
line_profiler module: | |||
* Can time code by adding decorators | |||
<source lang="python"> | |||
@profile | |||
def slow_function(a, b, c): | |||
... | |||
</source> | |||
This results in a file called <code>script_to_profile.py.lprof</code>, which you can read with the line_prof module: | |||
<pre> | |||
$ python -m line_profiler script_to_profile.py.lprof | |||
</pre> | |||
Link to github: https://github.com/rkern/line_profiler | |||
===Graphviz Dot=== | |||
Gprof2dot: | Gprof2dot: | ||
Revision as of 04:50, 21 March 2017
Introduction
List of Python Profiling Tools
Built Into Python
Profiling tools provided by Python:
- cProfile is a C extension, the recommended method of profiling python programs, and is based on lsprof
profile is a module that adds significant overhead; reports time spent in calls to built-in functions and methodshotshot - not developed/supported anymore
Benefits:
- Easy
- Built-in
Downsides:
- As stated by rkern [1], "The current profiling tools supported in Python 2.7 and later only time function calls. This is a good first step for locating hotspots in one's program and is frequently all one needs to do to optimize the program. However, sometimes the cause of the hotspot is actually a single line in the function, and that line may not be obvious from just reading the source code."
Link: https://docs.python.org/2/library/profile.html
Line by Line
line_profiler module:
- Can time code by adding decorators
@profile
def slow_function(a, b, c):
...
This results in a file called script_to_profile.py.lprof, which you can read with the line_prof module:
$ python -m line_profiler script_to_profile.py.lprof
Link to github: https://github.com/rkern/line_profiler
Graphviz Dot
Gprof2dot:
- Reads all sorts of input formats, outputs to graphviz dot
Gprof2dot link: https://github.com/jrfonseca/gprof2dot