|
161 | 161 | "name": "stdout", |
162 | 162 | "output_type": "stream", |
163 | 163 | "text": [ |
164 | | - "1 loop, best of 3: 1.94 s per loop\n" |
| 164 | + "3.07 s ± 15.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" |
165 | 165 | ] |
166 | 166 | } |
167 | 167 | ], |
|
189 | 189 | "cell_type": "markdown", |
190 | 190 | "metadata": {}, |
191 | 191 | "source": [ |
192 | | - "With the \"raw\" Python code above, the best execution time achieved was 1.94 seconds. Keep in mind that with these three nested loops, that the statements inside the **j** loop are being evaluated more than 650,000 times. Let's compare that with the performance of the same code implemented with array operations:" |
| 192 | + "With the \"raw\" Python code above, the mean execution time achieved was 3.07 seconds (on a MacBook Pro Mid 2012). Keep in mind that with these three nested loops, that the statements inside the **j** loop are being evaluated more than 650,000 times. Let's compare that with the performance of the same code implemented with array operations:" |
193 | 193 | ] |
194 | 194 | }, |
195 | 195 | { |
|
201 | 201 | "name": "stdout", |
202 | 202 | "output_type": "stream", |
203 | 203 | "text": [ |
204 | | - "100 loops, best of 3: 5.09 ms per loop\n" |
| 204 | + "7.38 ms ± 105 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
205 | 205 | ] |
206 | 206 | } |
207 | 207 | ], |
|
212 | 212 | "\n", |
213 | 213 | "for n in range(nt + 1): ##loop across number of time steps\n", |
214 | 214 | " un = u.copy()\n", |
215 | | - " u[1:, 1:] = un[1:, 1:] - ((c * dt / dx * (un[1:, 1:] - un[1:, 0:-1])) -\n", |
| 215 | + " u[1:, 1:] = (un[1:, 1:] - (c * dt / dx * (un[1:, 1:] - un[1:, 0:-1])) -\n", |
216 | 216 | " (c * dt / dy * (un[1:, 1:] - un[0:-1, 1:])))\n", |
217 | 217 | " u[0, :] = 1\n", |
218 | 218 | " u[-1, :] = 1\n", |
|
224 | 224 | "cell_type": "markdown", |
225 | 225 | "metadata": {}, |
226 | 226 | "source": [ |
227 | | - "As you can see, the speed increase is substantial. The same calculation goes from 1.94 seconds to 5.09 milliseconds. 2 seconds isn't a huge amount of time to wait, but these speed gains will increase exponentially with the size and complexity of the problem being evaluated. " |
| 227 | + "As you can see, the speed increase is substantial. The same calculation goes from 3.07 seconds to 7.38 milliseconds. 3 seconds isn't a huge amount of time to wait, but these speed gains will increase exponentially with the size and complexity of the problem being evaluated. " |
228 | 228 | ] |
229 | 229 | }, |
230 | 230 | { |
|
353 | 353 | "name": "python", |
354 | 354 | "nbconvert_exporter": "python", |
355 | 355 | "pygments_lexer": "ipython3", |
356 | | - "version": "3.6.5" |
| 356 | + "version": "3.6.8" |
357 | 357 | } |
358 | 358 | }, |
359 | 359 | "nbformat": 4, |
|
0 commit comments