Python Code
profiling
JACKSON ISAAC
S7 B.TECH CSE
AMRITA VISHWA VIDYAPEETHAM, INDIA
What Is Profiling?
 Analysis of a program to measure the memory used by a certain module,
frequency and duration of function calls and the time complexity of the
same.
 Such profiling tools are termed as ‘profilers’.
Development tools in python
 Line_profiler
 Memory_profiler
 Clone digger
History of line_Profiler
 Initial Release v1.0b1 (2008) by Robert Kern.
 v1.0b2 (2009) – Fixes related to timing overflow on Windows.
 v1.0b3 (2011) – Update Compatibility for newer versions of Cython and
IPython.
 v1.0 (2014, stable release) – Added compatibility for Python 3.x
 Previously supported by kmike’s bitbucket fork of line_profiler.
Installing line_profiler
 Using pip – “pip install line_profiler”
 From source:
 git clone https://github.com/rkern/line_profiler.git
 cd line_profiler
 sudo python setup.py install (for python 2.7.x)
 sudo python3 setup.py install (for python3)
Using Line_PRofiler
 Adding profiler to the code:
@profile
def foo(bar):
…
 Running line_profiler:
 kernprof.py -l example.py
 python -m line_profiler example.py.lprof
 python3 -m line_profiler example.py.lprof
Live DEMO !
Understanding output
 Function : Displays the name of the function which is profiled and its line number.
 Line# : Line number of the code in the respective file.
 Hits : Number of times the code in the respective line was executed.
 Time : Total amount of time spent in executing the line in ‘Timer unit’ (i.e 1e-06s here). May
vary from system to system.
 Per Hit : The average amount of time spent in executing the line once in Timer Unit.
 % Time : The percentage of time spent on that line with respect to the total amount of
recorded time spent in the function.
 Line Contents : It displays the actual source code.
advantages
 Line_profiler helps us to point out which module of our code is running
slower than expected by profiling the time taken by each line of code.
 Helps in testing large projects, time spent by modules to execute a
particular function.
 Using this data we can modify and improve our code to build faster and
better programs and softwares.
Contact
 Website: isaacjackson.me
 Blog: www.jacksonisaac.wordpress.com
 Email: jacksonisaac2008@gmail.com
 Twitter/iJacksonIsaac
 LinkedIn/JacksonIsaac
 github.com/jacksonisaac
 Mozillian/ www.mozillians.org/en-US/u/jacksonisaac2008/
Thank You

Python code profiling - Jackson Isaac

  • 1.
    Python Code profiling JACKSON ISAAC S7B.TECH CSE AMRITA VISHWA VIDYAPEETHAM, INDIA
  • 2.
    What Is Profiling? Analysis of a program to measure the memory used by a certain module, frequency and duration of function calls and the time complexity of the same.  Such profiling tools are termed as ‘profilers’.
  • 3.
    Development tools inpython  Line_profiler  Memory_profiler  Clone digger
  • 4.
    History of line_Profiler Initial Release v1.0b1 (2008) by Robert Kern.  v1.0b2 (2009) – Fixes related to timing overflow on Windows.  v1.0b3 (2011) – Update Compatibility for newer versions of Cython and IPython.  v1.0 (2014, stable release) – Added compatibility for Python 3.x  Previously supported by kmike’s bitbucket fork of line_profiler.
  • 5.
    Installing line_profiler  Usingpip – “pip install line_profiler”  From source:  git clone https://github.com/rkern/line_profiler.git  cd line_profiler  sudo python setup.py install (for python 2.7.x)  sudo python3 setup.py install (for python3)
  • 6.
    Using Line_PRofiler  Addingprofiler to the code: @profile def foo(bar): …  Running line_profiler:  kernprof.py -l example.py  python -m line_profiler example.py.lprof  python3 -m line_profiler example.py.lprof
  • 7.
  • 8.
    Understanding output  Function: Displays the name of the function which is profiled and its line number.  Line# : Line number of the code in the respective file.  Hits : Number of times the code in the respective line was executed.  Time : Total amount of time spent in executing the line in ‘Timer unit’ (i.e 1e-06s here). May vary from system to system.  Per Hit : The average amount of time spent in executing the line once in Timer Unit.  % Time : The percentage of time spent on that line with respect to the total amount of recorded time spent in the function.  Line Contents : It displays the actual source code.
  • 9.
    advantages  Line_profiler helpsus to point out which module of our code is running slower than expected by profiling the time taken by each line of code.  Helps in testing large projects, time spent by modules to execute a particular function.  Using this data we can modify and improve our code to build faster and better programs and softwares.
  • 10.
    Contact  Website: isaacjackson.me Blog: www.jacksonisaac.wordpress.com  Email: [email protected]  Twitter/iJacksonIsaac  LinkedIn/JacksonIsaac  github.com/jacksonisaac  Mozillian/ www.mozillians.org/en-US/u/jacksonisaac2008/
  • 11.