From charlesreid1

Line 23: Line 23:
line_profiler module:  
line_profiler module:  
* Can time code by adding decorators
* Can time code by adding decorators
* Dumps a binary file that gives a line by line breakdown of time spent within a given function
To install:
<pre>
$ pip install line_profiler
</pre>
To profile:


<source lang="python">
<source lang="python">
Line 37: Line 46:


Link to github: https://github.com/rkern/line_profiler
Link to github: https://github.com/rkern/line_profiler


===Graphviz Dot===
===Graphviz Dot===

Revision as of 05:07, 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 methods
  • hotshot - 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
  • Dumps a binary file that gives a line by line breakdown of time spent within a given function

To install:

$ pip install line_profiler

To profile:

@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