You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Even when we measure the total time that a function takes to run (#43), that doesn't help us with knowing which parts of the code are slow!
To look into that, we need to use a different too called a profiler. Python comes with its own profiler, but we will use a more convenient tool.
Setup
This exercise will work with IPython or Jupyter notebooks, and will use two "magic" commands available there. You may need some steps to set them up first.
If you use Anaconda, you should already have access to Jupyter. If you don't, let us know on Moodle or use pip install ipython to install IPython.
The %prun magic should be already available with every installation of IPython/Jupyter. However, you may need to install the second magic (%lprun).
If you use Anaconda, run conda install line_profiler from a terminal. Otherwise, use pip install line_profiler.
Using profiling tools in IPython/Jupyter notebook
prun's magic gives us information about every function called.
defsum_of_lists(N):
total=0foriinrange(5):
L= [j^ (j>>i) forjinrange(N)]
# j >> i == j // 2 ** i (shift j bits i places to the right)# j ^ i -> bitwise exclusive or; j's bit doesn't change if i's = 0, changes to complement if i's = 1total+=sum(L)
returntotal
run %prun:
%prunsum_of_lists(10_000_000)
Look at the table of results. What information does it give you? Can you find which operation takes the most time? (You may find it useful to look at the last column first)
Using a line profiler in IPython/Jupyter
While prun presents its results by function, the lprun magic gives us line-by-line details.
Load the extension on your IPython shell or Jupyter notebook
%load_extline_profiler
Run %lprun
%lprun-fsum_of_listssum_of_lists(10_000_000)
Can you interpret the results? On which line is most of the time spent?
Finishing up
When you are done, react to this issue using one of the available emojis, and/or comment with your findings: Which function takes the most time? Which line of the code?
The text was updated successfully, but these errors were encountered:
In [3]: %lprun-fsum_of_listssum_of_lists(10_000_000)
Timerunit: 1e-09sTotaltime: 11.7898sFile: <ipython-input-2-5de972046bd3>Function: sum_of_listsatline1Line# Hits Time Per Hit % Time Line Contents==============================================================1defsum_of_lists(N):
21637.0637.00.0total=0367876.01312.70.0foriinrange(5):
4500000051e+10230.197.6L= [j^ (j>>i) forjinrange(N)]
5# j >> i == j // 2 ** i (shift j bits i places to the right)6# j ^ i -> bitwise exclusive or; j's bit doesn't change if i's = 0, changes to complement if i's = 175284864757.06e+072.4total+=sum(L)
811376.01376.00.0returntotal
Even when we measure the total time that a function takes to run (#43), that doesn't help us with knowing which parts of the code are slow!
To look into that, we need to use a different too called a profiler. Python comes with its own profiler, but we will use a more convenient tool.
Setup
This exercise will work with IPython or Jupyter notebooks, and will use two "magic" commands available there. You may need some steps to set them up first.
If you use Anaconda, you should already have access to Jupyter. If you don't, let us know on Moodle or use
pip install ipython
to install IPython.The
%prun
magic should be already available with every installation of IPython/Jupyter. However, you may need to install the second magic (%lprun
).If you use Anaconda, run
conda install line_profiler
from a terminal. Otherwise, usepip install line_profiler
.Using profiling tools in IPython/Jupyter notebook
prun
's magic gives us information about every function called.%prun
:Using a line profiler in IPython/Jupyter
While
prun
presents its results by function, thelprun
magic gives us line-by-line details.%lprun
Finishing up
When you are done, react to this issue using one of the available emojis, and/or comment with your findings: Which function takes the most time? Which line of the code?
The text was updated successfully, but these errors were encountered: