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
However, broadcasting isnot just a nicer way to write mathematical expressions—it can also give a significant performance boost:
181
181
Most modern processors are able to apply one instruction across multiple variables simultaneously, instead of sequentially. (In computer science, this is also referred to as"vectorisation".) The manner by which NumPy stores data in arrays enables it to vectorise mathematical operations that are broadcast across arrays.
182
182
183
-
<!-- Analogy: If you're baking cookies, the oven (CPU register) is big enough to operate on multiple cookies (numbers) simultaneously. So whether you bake 1 cookie or 10, it'll take exactly the same amount of time. -->
@@ -193,6 +192,18 @@ Most modern processors are able to apply one instruction across multiple variabl
193
192
If we were to use a regular `for` loop, the time to perform this operation would increase with the length of the array.
194
193
However, using NumPy broadcasting we can apply the addition to 1, 10 or 100 elements, all in the same amount of time!
195
194
195
+
::::::::::::::::::::::::::::::::::::: instructor
196
+
197
+
A simple analogy:
198
+
199
+
If you're baking cookies, the oven (CPU register) is big enough to operate on multiple cookies (numbers) simultaneously. So whether you bake 1 cookie or 10, it'll take exactly the same amount of time.
200
+
However, this requires that the cookies are neatly arranged on a baking tray (in a contiguous chunk of memory).
201
+
202
+
Basic ints/floats in NumPy arrays are arranged like that, so this works great.
203
+
In contrast, numbers in a Python list [are spread across memory in a fairly complex arrangement](https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/#3.-Python's-object-model-can-lead-to-inefficient-memory-access), so cannot benefit from this unless you convert them to a NumPy array first.
204
+
205
+
::::::::::::::::::::::::::::::::::::::::::::::::
206
+
196
207
Earlier it was demonstrated that using core Python methods over a list will outperform a loop, performing the same calculation faster. The below example takes this a step further by demonstrating the calculation of a dot product.
197
208
198
209
<!-- Inspired by High Performance Python Chapter 6 example
@@ -324,7 +335,6 @@ To vectorise this efficiently, the logic of the code had to be changed slightly:
324
335
::::::::::::::::::::::::::::::::::::: instructor
325
336
326
337
The following code snippet demonstrates how this works for a simplified example.
327
-
If you want to run this as a live demo, you need to `pip install shapely` first.
To complete some of the exercises you will need to use a text-editor or Python IDE, so make sure you have your favourite available.
35
35
36
+
:::::::::::::: instructor
37
+
38
+
As the instructor, you should additionally install the `shapely` package, which you may need for a brief demo during the episode on scientific Python packages.
0 commit comments