-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofiling.tex
279 lines (201 loc) · 6.05 KB
/
profiling.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
\documentclass[serif]{beamer}
%\usepackage[utopia]{mathdesign}
%\usepackage[no-math]{fontspec}
%\setmainfont{Liberation Serif}
\usepackage{minted}
\usepackage{hyperref}
\usepackage{ccicons}
\usepackage{ulem}
% \usepackage{tikz}
% \usetikzlibrary{arrows,shapes,snakes,automata,backgrounds,petri}
%gets rid of bottom navigation bars
\setbeamertemplate{footline}[page number]{}
%gets rid of navigation symbols
\setbeamertemplate{navigation symbols}{}
\title{Profiling python code}
\author{Zbigniew Jędrzejewski-Szmek}
\institute{%
\includegraphics[width=10em]{images/Logo-redhat-color-375.png}\\
\medskip
\textit{zbyszek@in.waw.pl}\\
\medskip
\ccbysa
}
\date{\tiny Stuttgart, 2020.01.21}
\begin{document}
\begin{frame}
\titlepage % Print the title page as the first slide
\end{frame}
\begin{frame}
\frametitle{Lecture links}
\url{https://github.com/IMS-workshop/profiling-lecture}
\end{frame}
\begin{frame}
\frametitle{What is optimization?}
``Program optimization or software optimization is the process of
modifying a software system to make some aspect of it work more
efficiently or use fewer resources.''
\hfill Wikipedia
\end{frame}
\begin{frame}
\frametitle{Premature optimization}
``Programmers waste enormous amounts of time thinking about, or
worrying about, the speed of noncritical parts of their programs,
and these attempts at efficiency actually have a strong negative
impact when debugging and maintenance are considered. We should
forget about small efficiencies, say about 97\% of the time:
premature optimization is the root of all evil. Yet we should not
pass up our opportunities in that critical 3\%.''
\hfill Donald Knuth
\end{frame}
\begin{frame}
\includegraphics[width=\textwidth]{images/is_it_worth_the_time_2x.png}
\hfill
\href{https://xkcd.com/1205/}{xkcd.com/1205}
\includegraphics[height=1em]{images/240px-Cc-by-nc_icon.png}
\end{frame}
\begin{frame}
``Optimization is the root of all evil.''
\hfill Donald Knuth
\end{frame}
\begin{frame}
\frametitle{Optimization workflow}
\begin{enumerate}
\item \textbf{Make it work}: write the code in a simple legible way
\item \textbf{Make it work reliably}: write automated test cases,
make really sure that your algorithm is right and that if you
break it, the tests will capture the breakage.
\item Optimize the code by profiling simple use-cases to
\textbf{find the bottlenecks} and speed up these bottlenecks,
finding a better algorithm, or implementation.
Keep in mind the tradeoff between simplicity and reliability
and speed of exection of the code.
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{\texttt{\%timeit}}
demo
\end{frame}
\begin{frame}
\frametitle{Exercise}
Please see \texttt{even-odds/exercise.txt}.
\end{frame}
\begin{frame}
\frametitle{Exercise!}
Make factorial faster by using a cache (e.g. a dict).
What about \texttt{functools.lru\_cache}?
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{time} shell builtin}
On *nix systems, the command time gives a quick way of measuring time:
\begin{minted}{console}
$ time find /usr -name idontexist
1.91s user 4.32s system 36% cpu 17.279 total
$ time sleep 5
0.00s user 0.00s system 0% cpu 5.005 total
\end{minted}
``total'' or ``real'' is wall clock time\\
``user'' is CPU time executing the script\\
``system'' or ``sys'' is CPU time spent in system calls
\end{frame}
\begin{frame}
\frametitle{Exercise}
Please see \texttt{pyc-files/exercise.txt}.
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{cProfile}}
standard Python module to profile an entire application\\
(profile is an old, slow profiling module)
\bigskip
Running the profiler from command line:
\begin{minted}{console}
$ python -m cProfile -s cumtime myscript.py
\end{minted}
\bigskip
Sorting options:
\begin{itemize}
\item
tottime : time spent in function only
\item
cumtime : time spent in function and sub-calls
\item
calls : number of calls
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{cProfile}}
Save results to disk:
\begin{minted}{console}
$ python -m cProfile -o filename.prof myscript.py
\end{minted}
Explore:
\begin{minted}{console}
$ python -m pstats filename.prof
filename.prof% help
stats [n | regexp]: print statistics
sort [cumulative, time, ...] : change sort order
callers [n | regexp]: show callers of functions
callees [n | regexp]: show callees of functions
...
\end{minted}
\end{frame}
\begin{frame}
\includegraphics[width=\textwidth]{factorial/kcachegrind.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{kernprof}}
\begin{minted}{console}
$ sudo dnf install 'python3dist(line-profiler)'
(or)
$ pip3 install --user line_profiler
\end{minted}
\bigskip
\begin{minted}{python}
@profile
def slow_function(a, b, **kwargs):
...
\end{minted}
\bigskip
\begin{minted}{console}
$ kernprof -l script_to_profile.py
$ python3 -m line_profiler script_to_profile.py.lprof
\end{minted}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{pysnooper}}
\begin{minted}{console}
$ sudo dnf install 'python3dist(pysnooper)'
(or)
$ pip3 install --user pysnooper
\end{minted}
\bigskip
\begin{minted}{python3}
import pysnooper
@pysnooper.snoop()
def funcition_to_trace(n):
...
\end{minted}
\bigskip
\begin{minted}{console}
$ python3 script_to_trace.py |& less
\end{minted}
\end{frame}
\begin{frame}[fragile]
\frametitle{Thanks to …}
\begin{itemize}
\item Pietro Berkes\\
{\small \url{https://github.com/ASPP/testing_debugging_profiling}}
\item Nelle Veroquaux\\
{\small \url{https://github.com/ASPP/2019-camerino-profiling-cython-numba}}
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Toolbox}
\url{https://docs.python.org/3/library/profile.html}
\url{https://kcachegrind.github.io/}
\url{https://github.com/rkern/line_profiler}
\url{https://github.com/cool-RR/PySnooper}
\url{https://pypi.python.org/pypi/memory_profiler}
\url{http://www.camillescott.org/2013/12/06/yep/}
\end{frame}
\end{document}