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
{{ message }}
Repository navigation
Commit 21f1ea0
Browse filesBrowse the repository at this point in the historyBrowse files
We will discuss these ideas extensively in this and the remaining lectures from
99
-
this series.
100
+
We will discuss these ideas in depth below.
100
101
101
102
102
103
### Python's Scientific Ecosystem
@@ -123,7 +124,7 @@ Here's how they fit together:
123
124
* Pandas provides types and functions for manipulating data.
124
125
* Numba provides a just-in-time compiler that plays well with NumPy and helps accelerate Python code.
125
126
126
-
We will discuss all of these libraries extensively in this lecture series.
127
+
We will discuss all of these libraries at length in this lecture series.
127
128
128
129
129
130
## Pure Python is slow
@@ -189,15 +190,13 @@ a, b = ['foo'], ['bar']
189
190
a + b
190
191
```
191
192
192
-
(We say that the operator `+` is *overloaded* --- its action depends on the
193
-
type of the objects on which it acts)
194
193
195
-
As a result, when executing `a + b`, Python must first check the type of the objects and then call the correct operation.
194
+
As a result, when executing `a + b`, Python must first check the type of the
195
+
objects and then call the correct operation.
196
196
197
-
This involves a nontrivial overhead.
197
+
This involves overhead.
198
198
199
-
If we repeatedly execute this expression in a tight loop, the nontrivial
200
-
overhead becomes a large overhead.
199
+
If we repeatedly execute this expression in a tight loop, the overhead becomes large.
201
200
202
201
203
202
#### Static types
@@ -243,38 +242,29 @@ To illustrate, let's consider the problem of summing some data --- say, a collec
243
242
244
243
#### Summing with Compiled Code
245
244
246
-
In C or Fortran, these integers would typically be stored in an array, which
247
-
is a simple data structure for storing homogeneous data.
245
+
In C or Fortran, an array of integers is stored in a single contiguous block of memory
248
246
249
-
Such an array is stored in a single contiguous block of memory
250
-
251
-
* In modern computers, memory addresses are allocated to each byte (one byte = 8 bits).
252
247
* For example, a 64 bit integer is stored in 8 bytes of memory.
253
248
* An array of $n$ such integers occupies $8n$ *consecutive* memory slots.
254
249
255
-
Moreover, the compiler is made aware of the data type by the programmer.
256
-
257
-
* In this case 64 bit integers
250
+
Moreover, the data type is known at compile time.
258
251
259
252
Hence, each successive data point can be accessed by shifting forward in memory
260
253
space by a known and fixed amount.
261
254
262
-
* In this case 8 bytes
263
255
264
256
#### Summing in Pure Python
265
257
266
258
Python tries to replicate these ideas to some degree.
267
259
268
-
For example, in the standard Python implementation (CPython), list elements are placed in memory locations that are in a sense contiguous.
260
+
For example, in the standard Python implementation (CPython), list elements are
261
+
placed in memory locations that are in a sense contiguous.
269
262
270
263
However, these list elements are more like pointers to data rather than actual data.
271
264
272
265
Hence, there is still overhead involved in accessing the data values themselves.
273
266
274
-
This is a considerable drag on speed.
275
-
276
-
In fact, it's generally true that memory traffic is a major culprit when it comes to slow execution.
277
-
267
+
Such overhead is a major culprit when it comes to slow execution.
278
268
279
269
280
270
### Summary
@@ -295,15 +285,11 @@ synonymous with parallelization.
295
285
296
286
This task is best left to specialized compilers!
297
287
298
-
Certain Python libraries have outstanding capabilities for parallelizing scientific code -- we'll discuss this more as we go along.
299
-
300
-
301
288
302
289
303
290
## Accelerating Python
304
291
305
-
In this section we look at three related techniques for accelerating Python
306
-
code.
292
+
In this section we look at three related techniques for accelerating Python code.
307
293
308
294
Here we'll focus on the fundamental ideas.
309
295
@@ -325,10 +311,11 @@ Many economists usually refer to array programming as "vectorization."
325
311
In computer science, this term has [a slightly different meaning](https://en.wikipedia.org/wiki/Automatic_vectorization).
326
312
```
327
313
328
-
The key idea is to send array processing operations in batch to pre-compiled
329
-
and efficient native machine code.
314
+
The key idea is to send array processing operations in batch to pre-compiled and
315
+
efficient native machine code.
330
316
331
-
The machine code itself is typically compiled from carefully optimized C or Fortran.
317
+
The machine code itself is typically compiled from carefully optimized C or
318
+
Fortran.
332
319
333
320
For example, when working in a high level language, the operation of inverting a
334
321
large matrix can be subcontracted to efficient machine code that is pre-compiled
@@ -346,6 +333,7 @@ The idea of vectorization dates back to MATLAB, which uses vectorization extensi
0 commit comments