forked from PythonCHB/PythonCertSpring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresentation-week08.tex
More file actions
328 lines (225 loc) · 7.5 KB
/
presentation-week08.tex
File metadata and controls
328 lines (225 loc) · 7.5 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
\documentclass{beamer}
%\usepackage[latin1]{inputenc}
\usetheme{Warsaw}
\title[Python Certificate: System Development]{System Development with Python: Week 8}
\author{Christopher Barker}
\institute{UW Continuing Education}
\date{May 14, 2013}
\usepackage{listings}
\usepackage{hyperref}
\begin{document}
% ---------------------------------------------
\begin{frame}
\titlepage
\end{frame}
% ---------------------------------------------
\begin{frame}
\frametitle{Table of Contents}
%\tableofcontents[currentsection]
\tableofcontents
\end{frame}
\section{Timing}
% ---------------------------------------------
\begin{frame}[fragile]{Performance Testing}
{\Large ``Premature optimization is the root of all evil''}\\[0.1in]
{\large \hspace{0.5in} -- Donald Knuth}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Profiling/timing}
\vfill
{\Large You can't optimize your code without knowing where the bottlenecks are.}
\vfill
{\Large Smarter people than me have said they they are almost always wrong
when they try to logically determine where the slow code is. (I know I am)}
\vfill
{\Large ... and how to speed it up}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{time.clock()}
{\Large The really easy way:}
\begin{verbatim}
import time
start = time.clock()
... do_some_stuff ...
print "It took %f seconds to run"%(time.clock - start)
\end{verbatim}
{\Large It works, it's easy, and it gives a gross approximation}
\vfill
(use \verb|time.clock()|, rather than \verb|time.time()|)
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{timeit}
{\Large The good way:}
\begin{verbatim}
import timeit
timeit.timeit( statement, setup=some_stuff)
\end{verbatim}
{\Large It's kind of a pain, but gives meaningful results.}
(can also be called on the command line)
\vfill
\url{http://docs.python.org/library/timeit.html}
\vfill
(\verb|code/timing.py|)
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{\%timeit}
{\Large The easy and good way:}
\vfill
{\LARGE \verb|ipython|:}
\begin{verbatim}
In [52]: import timing
In [53]: %timeit timing.primes_stupid(5)
100000 loops, best of 3: 10.9 us per loop
\end{verbatim}
{\Large Takes care of the setup/namespace stuff for you}
\vfill
\url{http://ipython.org/ipython-doc/dev/interactive/tutorial.html}
\end{frame}
\section{Profiling}
% ---------------------------------------------
\begin{frame}[fragile]{Profiling}
{\Large A profiler is a tool that describes the run time performance of a
program, providing a variety of statistics}
\vfill
{\Large Helpful when you don't yet know where your bottlenecks are}
\vfill
{\Large The python profiler}
\begin{verbatim}
python -m cProfile profile_example.py
\end{verbatim}
{\Large spews some stats}
\vfill
\url{http://docs.python.org/library/profile.html}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{python profiler}
{\Large What you get:}
\begin{description}
\item[ncalls] the number of calls.
\item[tottime] the total time spent in the given function (and excluding time made in calls to sub-functions),
\item[percall] the quotient of tottime divided by ncalls
\item[cumtime] the total time spent in this and all subfunctions (from invocation till exit). This figure is accurate even for recursive functions.
\item[percall] the quotient of cumtime divided by primitive calls
\end{description}
(demo: \verb|python -m cProfile profile_example.py|)
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{python profiler}
{\Large You can also dump to a file:}
\vfill
{\verb|$ python -m cProfile -o profile_dump profile_example.py|}
\vfill
{\large This gives you a binary file you can examine with \verb|pstats|:}
\vfill
{demo: \verb|$ python -m pstats|}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{pstats}
{\Large Running \verb|pstats|}
{\small
\begin{verbatim}
$
$ python -m pstats
Welcome to the profile statistics browser.
% read profile_dump
profile_dump% stats
Wed Aug 29 16:21:39 2012 profile_dump
51403 function calls in 0.032 seconds
Random listing order was used
ncalls tottime percall cumtime percall filename:lineno(function)
51200 0.006 0.000 0.006 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.032 0.032 profile_example.py:9(<module>)
1 0.001 0.001 0.032 0.032 profile_example.py:28(main)
100 0.022 0.000 0.027 0.000 profile_example.py:11(odd_numbers)
100 0.003 0.000 0.031 0.000 profile_example.py:19(sum_odd_numbers)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
profile_dump%
\end{verbatim}
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{pstats commands}
{\Large Commands:}
\begin{description}
\item[help] help on pstats or particular command
\item[stats] print the profile statistics
\item[sort] sort by various data fields
\item[strip] strips the leading path info from file names
\item[callers] Print callers statistics
\item[callees] Print callees statistics
\item[quit] quits
\end{description}
{\large Each has options to customize output}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{automating profile stats}
{\Large \verb|cProfile| and \verb|pstats| are also modules}
\vfill
{\Large So you can script collection of profiles and stats}
\vfill
\url{http://docs.python.org/library/profile.html}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{``Run Snake Run''}
\vfill
{\Large For a visual look at your profiling results:}
\vfill
\url{http://www.vrplumber.com/programming/runsnakerun/}
\vfill
(pretty cool stuff!)
\end{frame}
\section{Performance Tuning}
% ---------------------------------------------
\begin{frame}[fragile]{Performance Tips}
\vfill
{\Large Some common python performance issues:}
\vfill
\url{http://wiki.python.org/moin/PythonSpeed/PerformanceTips/}
\vfill
(some nifty profiling tools described there, too)
\end{frame}
%-------------------------------
\begin{frame}[fragile]{LAB}
{\Large Profiling lab}
\begin{itemize}
\item run \verb|timeit| on some code of yours (or timing.py, or..)
\item run iPython's \%timeit on the same code.
\item try to make the factorial code in timing.py faster, and time the difference.
\item write some code that tests one of the performance issues in:\\
{\small \url{http://wiki.python.org/moin/PythonSpeed/PerformanceTips} }\\
use one of the \verb|timeit|s to see if you can make a difference.
\item try the profile tutorial at:\\
{\small \url{http://pysnippet.blogspot.com/2009/12/profiling-your-python-code.html} }
\end{itemize}
\end{frame}
%-------------------------------
\begin{frame}[fragile]{Wrap up}
\vfill
{\Large I hope you have an idea how to profile and time your code.}
\vfill
{\Large Try it on a part of your project}
\vfill
\end{frame}
%-------------------------------
\begin{frame}{Next Week:}
\vfill
{\LARGE Student Presentations}
\vfill
\end{frame}
% -------------------------------
\begin{frame}[fragile]{Homework}
\vfill
\Large{Profile your project}
\vfill
\Large{Performance tune part of it}
\vfill
\Large{Get ready to present}
\vfill
\end{frame}
%-------------------------------
\begin{frame}[fragile]{Project Time!}
\vfill
\Large{Map out what you're going to present}
\vfill
\end{frame}
\end{document}