Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile python 3.5 code line by line in jupyter notebook 5

Tags:

How to find out execution time taken by each line of python code.

line_profiler works with ipython but doesnt work with jupyter notebook. I tried adding @profile to my function, it gives error saying name 'profile' is not defined. There is one way to do it by time.time() , but i was wondering if there is any inbuilt profiling function which can profile each line of my function and show me the execution time.

def prof_function():
    x=10*20
    y=10+x
    return (y)
like image 293
Aseem Avatar asked Jun 24 '17 08:06

Aseem


Video Answer


1 Answers

You can use line_profiler in jupyter notebook.

  1. Install it: pip install line_profiler
  2. Within your jupyter notebook, call: %load_ext line_profiler
  3. Define your function prof_function as in your example.
  4. Finally, profile as follows: %lprun -f prof_function prof_function()

Which will provide the output:

Timer unit: 1e-06 s

Total time: 3e-06 s
File: <ipython-input-22-41854af628da>
Function: prof_function at line 1

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     1                                           def prof_function():
     2         1          1.0      1.0     33.3      x=10*20
     3         1          1.0      1.0     33.3      y=10+x
     4         1          1.0      1.0     33.3      return (y)
like image 98
S.A. Avatar answered Sep 17 '22 17:09

S.A.