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)
You can use line_profiler
in jupyter notebook.
pip install line_profiler
%load_ext line_profiler
prof_function
as in your example. %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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With