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
Copy file name to clipboardExpand all lines: lectures/need_for_speed.md
+31-35Lines changed: 31 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,10 +27,10 @@ premature optimization is the root of all evil." -- Donald Knuth
27
27
28
28
## Overview
29
29
30
-
Python is extremely popular for scientific computing, due to such factors as
30
+
Python is popular for scientific computing due to factors such as
31
31
32
32
* the accessible and expressive nature of the language itself,
33
-
*its vast range of high quality scientific libraries,
33
+
*the huge range of high quality scientific libraries,
34
34
* the fact that the language and libraries are open source,
35
35
* the popular [Anaconda Python distribution](https://www.anaconda.com/download), which simplifies installation and management of scientific libraries, and
36
36
* the key role that Python plays in data science, machine learning and artificial intelligence.
@@ -70,7 +70,9 @@ One reason we use scientific libraries is because they implement routines we wan
70
70
71
71
For example, it's almost always better to use an existing routine for root finding than to write a new one from scratch.
72
72
73
-
(For standard algorithms, efficiency is maximized if the community can coordinate on a common set of implementations, written by experts and tuned by users to be as fast and robust as possible.)
73
+
(For standard algorithms, efficiency is maximized if the community can
74
+
coordinate on a common set of implementations, written by experts and tuned by
75
+
users to be as fast and robust as possible.)
74
76
75
77
But this is not the only reason that we use Python's scientific libraries.
76
78
@@ -93,9 +95,9 @@ At QuantEcon, the scientific libraries we use most often are
93
95
*[NumPy](https://numpy.org/)
94
96
*[SciPy](https://scipy.org/)
95
97
*[Matplotlib](https://matplotlib.org/)
98
+
*[JAX](https://github.com/jax-ml/jax)
96
99
*[Pandas](https://pandas.pydata.org/)
97
100
*[Numba](https://numba.pydata.org/) and
98
-
*[JAX](https://github.com/jax-ml/jax)
99
101
100
102
Here's how they fit together:
101
103
@@ -104,14 +106,11 @@ Here's how they fit together:
104
106
multiplication).
105
107
* SciPy builds on NumPy by adding numerical methods routinely used in science (interpolation, optimization, root finding, etc.).
106
108
* Matplotlib is used to generate figures, with a focus on plotting data stored in NumPy arrays.
107
-
* Pandas provides types and functions for manipulating data.
108
-
* Numba provides a just-in-time compiler that integrates well with NumPy and
109
-
helps accelerate Python code.
110
109
* JAX includes array processing operations similar to NumPy, automatic
111
110
differentiation, a parallelization-centric just-in-time compiler, and automated integration with hardware accelerators such as
112
111
GPUs.
113
-
114
-
112
+
* Pandas provides types and functions for manipulating data.
113
+
* Numba provides a just-in-time compiler that plays well with NumPy and helps accelerate Python code.
115
114
116
115
117
116
## The Need for Speed
@@ -133,25 +132,27 @@ Indeed, the standard implementation of Python (called CPython) cannot match the
133
132
134
133
Does that mean that we should just switch to C or Fortran for everything?
135
134
136
-
The answer is: No, no, and one hundred times no!
137
-
138
-
(This is what you should say to your professor when they insist that your model needs to be rewritten in Fortran or C++.)
135
+
The answer is: No!
139
136
140
-
There are two reasons why:
137
+
There are three reasons why:
141
138
142
139
First, for any given program, relatively few lines are ever going to be time-critical.
143
140
144
141
Hence it is far more efficient to write most of our code in a high productivity language like Python.
145
142
146
143
Second, even for those lines of code that *are* time-critical, we can now achieve the same speed as C or Fortran using Python's scientific libraries.
147
144
148
-
In fact we can often do better, because some scientific libraries are so
149
-
effective at accelerating and parallelizing our code.
145
+
Third, in the last few years, accelerating code has become essentially
146
+
synonymous with parallelizing execution, and this task is best left to
147
+
specialized compilers.
148
+
149
+
Certain Python libraries have outstanding capabilities for parallelizing
150
+
scientific code -- we'll discuss this more as we go along.
150
151
151
152
152
153
### Where are the Bottlenecks?
153
154
154
-
Before we learn how to do this, let's try to understand why plain vanilla Python is slower than C or Fortran.
155
+
Before we do so, let's try to understand why plain vanilla Python is slower than C or Fortran.
155
156
156
157
This will, in turn, help us figure out how to speed things up.
157
158
@@ -275,17 +276,22 @@ Let's look at some ways around these problems.
275
276
```{index} single: Python; Vectorization
276
277
```
277
278
278
-
There is a clever method called **vectorization** that can be
279
-
used to speed up high level languages in numerical applications.
279
+
One method for avoiding memory traffic and type checking is [array programming](https://en.wikipedia.org/wiki/Array_programming).
280
+
281
+
Economists usually refer to array programming as ``vectorization.''
282
+
283
+
(In computer science, this term has [a slightly different meaning](https://en.wikipedia.org/wiki/Automatic_vectorization).)
280
284
281
285
The key idea is to send array processing operations in batch to pre-compiled
282
286
and efficient native machine code.
283
287
284
288
The machine code itself is typically compiled from carefully optimized C or Fortran.
285
289
286
-
For example, when working in a high level language, the operation of inverting a large matrix can be subcontracted to efficient machine code that is pre-compiled for this purpose and supplied to users as part of a package.
290
+
For example, when working in a high level language, the operation of inverting a
291
+
large matrix can be subcontracted to efficient machine code that is pre-compiled
292
+
for this purpose and supplied to users as part of a package.
287
293
288
-
This clever idea dates back to MATLAB, which uses vectorization extensively.
294
+
This idea dates back to MATLAB, which uses vectorization extensively.
0 commit comments