Skip to content

Commit 9b10892

Browse files
committed
updated materials
1 parent 93aa3dd commit 9b10892

16 files changed

Lines changed: 3107 additions & 952 deletions

4-AcceleratedPython/numba/2_Numba_Basics.ipynb renamed to 4-AcceleratedPython/numba/1_Numba_Basics.ipynb

Lines changed: 188 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
"source": [
77
"# Using `jit`\n",
88
"\n",
9-
"We know how to find hotspots now, how do we improve their performance?\n",
10-
"\n",
11-
"We `jit` them!\n",
12-
"\n",
139
"We'll start with a trivial example but get to some more realistic applications shortly."
1410
]
1511
},
@@ -24,7 +20,7 @@
2420
},
2521
{
2622
"cell_type": "code",
27-
"execution_count": null,
23+
"execution_count": 1,
2824
"metadata": {
2925
"collapsed": true
3026
},
@@ -44,7 +40,7 @@
4440
},
4541
{
4642
"cell_type": "code",
47-
"execution_count": null,
43+
"execution_count": 2,
4844
"metadata": {
4945
"collapsed": true
5046
},
@@ -55,7 +51,7 @@
5551
},
5652
{
5753
"cell_type": "code",
58-
"execution_count": null,
54+
"execution_count": 3,
5955
"metadata": {
6056
"collapsed": false
6157
},
@@ -66,22 +62,41 @@
6662
},
6763
{
6864
"cell_type": "code",
69-
"execution_count": null,
65+
"execution_count": 4,
7066
"metadata": {
7167
"collapsed": false
7268
},
73-
"outputs": [],
69+
"outputs": [
70+
{
71+
"data": {
72+
"text/plain": [
73+
"45041.071854295071"
74+
]
75+
},
76+
"execution_count": 4,
77+
"metadata": {},
78+
"output_type": "execute_result"
79+
}
80+
],
7481
"source": [
7582
"sum_array(arr)"
7683
]
7784
},
7885
{
7986
"cell_type": "code",
80-
"execution_count": null,
87+
"execution_count": 5,
8188
"metadata": {
8289
"collapsed": false
8390
},
84-
"outputs": [],
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"10 loops, best of 3: 20.5 ms per loop\n"
97+
]
98+
}
99+
],
85100
"source": [
86101
"plain = %timeit -o sum_array(arr)"
87102
]
@@ -95,7 +110,7 @@
95110
},
96111
{
97112
"cell_type": "code",
98-
"execution_count": null,
113+
"execution_count": 6,
99114
"metadata": {
100115
"collapsed": true
101116
},
@@ -113,7 +128,7 @@
113128
},
114129
{
115130
"cell_type": "code",
116-
"execution_count": null,
131+
"execution_count": 7,
117132
"metadata": {
118133
"collapsed": true
119134
},
@@ -131,33 +146,63 @@
131146
},
132147
{
133148
"cell_type": "code",
134-
"execution_count": null,
149+
"execution_count": 8,
135150
"metadata": {
136151
"collapsed": false
137152
},
138-
"outputs": [],
153+
"outputs": [
154+
{
155+
"data": {
156+
"text/plain": [
157+
"45041.07185429507"
158+
]
159+
},
160+
"execution_count": 8,
161+
"metadata": {},
162+
"output_type": "execute_result"
163+
}
164+
],
139165
"source": [
140166
"sum_array_numba(arr)"
141167
]
142168
},
143169
{
144170
"cell_type": "code",
145-
"execution_count": null,
171+
"execution_count": 9,
146172
"metadata": {
147173
"collapsed": false
148174
},
149-
"outputs": [],
175+
"outputs": [
176+
{
177+
"name": "stdout",
178+
"output_type": "stream",
179+
"text": [
180+
"10000 loops, best of 3: 86.2 µs per loop\n"
181+
]
182+
}
183+
],
150184
"source": [
151185
"jitted = %timeit -o sum_array_numba(arr)"
152186
]
153187
},
154188
{
155189
"cell_type": "code",
156-
"execution_count": null,
190+
"execution_count": 10,
157191
"metadata": {
158192
"collapsed": false
159193
},
160-
"outputs": [],
194+
"outputs": [
195+
{
196+
"data": {
197+
"text/plain": [
198+
"238.1571011913437"
199+
]
200+
},
201+
"execution_count": 10,
202+
"metadata": {},
203+
"output_type": "execute_result"
204+
}
205+
],
161206
"source": [
162207
"plain.best / jitted.best"
163208
]
@@ -171,7 +216,7 @@
171216
},
172217
{
173218
"cell_type": "code",
174-
"execution_count": null,
219+
"execution_count": 11,
175220
"metadata": {
176221
"collapsed": true
177222
},
@@ -191,22 +236,41 @@
191236
},
192237
{
193238
"cell_type": "code",
194-
"execution_count": null,
239+
"execution_count": 12,
195240
"metadata": {
196241
"collapsed": false
197242
},
198-
"outputs": [],
243+
"outputs": [
244+
{
245+
"data": {
246+
"text/plain": [
247+
"45041.07185429507"
248+
]
249+
},
250+
"execution_count": 12,
251+
"metadata": {},
252+
"output_type": "execute_result"
253+
}
254+
],
199255
"source": [
200256
"sum_array(arr)"
201257
]
202258
},
203259
{
204260
"cell_type": "code",
205-
"execution_count": null,
261+
"execution_count": 13,
206262
"metadata": {
207263
"collapsed": false
208264
},
209-
"outputs": [],
265+
"outputs": [
266+
{
267+
"name": "stdout",
268+
"output_type": "stream",
269+
"text": [
270+
"10000 loops, best of 3: 89.1 µs per loop\n"
271+
]
272+
}
273+
],
210274
"source": [
211275
"%timeit sum_array(arr)"
212276
]
@@ -220,15 +284,112 @@
220284
},
221285
{
222286
"cell_type": "code",
223-
"execution_count": null,
287+
"execution_count": 14,
224288
"metadata": {
225289
"collapsed": false
226290
},
227-
"outputs": [],
291+
"outputs": [
292+
{
293+
"name": "stdout",
294+
"output_type": "stream",
295+
"text": [
296+
"The slowest run took 5.33 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
297+
"10000 loops, best of 3: 40.7 µs per loop\n"
298+
]
299+
}
300+
],
228301
"source": [
229302
"%timeit arr.sum()"
230303
]
231304
},
305+
{
306+
"cell_type": "markdown",
307+
"metadata": {},
308+
"source": [
309+
"## When is Numba faster than NumPy?\n",
310+
"\n",
311+
"When doing more complex things, or when using less common integer types, like int16:"
312+
]
313+
},
314+
{
315+
"cell_type": "code",
316+
"execution_count": 15,
317+
"metadata": {
318+
"collapsed": false
319+
},
320+
"outputs": [],
321+
"source": [
322+
"arr_int16 = (arr * 4096).astype(numpy.int16)"
323+
]
324+
},
325+
{
326+
"cell_type": "code",
327+
"execution_count": 17,
328+
"metadata": {
329+
"collapsed": false
330+
},
331+
"outputs": [
332+
{
333+
"name": "stdout",
334+
"output_type": "stream",
335+
"text": [
336+
"10000 loops, best of 3: 20 µs per loop\n"
337+
]
338+
}
339+
],
340+
"source": [
341+
"jitted_int16 = %timeit -o sum_array_numba(arr_int16)"
342+
]
343+
},
344+
{
345+
"cell_type": "code",
346+
"execution_count": 18,
347+
"metadata": {
348+
"collapsed": false
349+
},
350+
"outputs": [
351+
{
352+
"name": "stdout",
353+
"output_type": "stream",
354+
"text": [
355+
"The slowest run took 7.39 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
356+
"10000 loops, best of 3: 108 µs per loop\n"
357+
]
358+
}
359+
],
360+
"source": [
361+
"numpy_int16 = %timeit -o arr_int16.sum()"
362+
]
363+
},
364+
{
365+
"cell_type": "code",
366+
"execution_count": 19,
367+
"metadata": {
368+
"collapsed": false
369+
},
370+
"outputs": [
371+
{
372+
"data": {
373+
"text/plain": [
374+
"5.420978311244756"
375+
]
376+
},
377+
"execution_count": 19,
378+
"metadata": {},
379+
"output_type": "execute_result"
380+
}
381+
],
382+
"source": [
383+
"numpy_int16.best / jitted_int16.best"
384+
]
385+
},
386+
{
387+
"cell_type": "markdown",
388+
"metadata": {},
389+
"source": [
390+
"NumPy doesn't have a specialized version of `sum()` for 16-bit integers, but Numba just generated one that was many times faster! Numba can take advantage of things like AVX support for packed integers while NumPy has to cast to a larger datatype to use one of the precompiled implementations."
391+
]
392+
},
232393
{
233394
"cell_type": "markdown",
234395
"metadata": {},
@@ -245,6 +406,7 @@
245406
}
246407
],
247408
"metadata": {
409+
"anaconda-cloud": {},
248410
"kernelspec": {
249411
"display_name": "Python [conda env:python3]",
250412
"language": "python",

0 commit comments

Comments
 (0)