@@ -257,8 +257,6 @@ When Numba cannot infer all type information, it will raise an error.
257
257
For example, in the case below, Numba is unable to determine the type of function ` mean ` when compiling the function ` bootstrap `
258
258
259
259
``` {code-cell} ipython3
260
- :tags: [raises-exception, hide-output]
261
-
262
260
@njit
263
261
def bootstrap(data, statistics, n):
264
262
bootstrap_stat = np.empty(n)
@@ -273,14 +271,17 @@ def mean(data):
273
271
274
272
data = np.array([2.3, 3.1, 4.3, 5.9, 2.1, 3.8, 2.2])
275
273
n_resamples = 10
276
- bootstrap(data, mean, n_resamples)
274
+
275
+ #Error
276
+ try:
277
+ bootstrap(data, mean, n_resamples)
278
+ except Exception as e:
279
+ print(e)
277
280
```
278
281
279
282
But Numba recognizes JIT-compiled functions
280
283
281
284
``` {code-cell} ipython3
282
- :tags: []
283
-
284
285
@njit
285
286
def mean(data):
286
287
return np.mean(data)
@@ -291,13 +292,13 @@ def mean(data):
291
292
We can check the signature of the JIT-compiled function
292
293
293
294
``` {code-cell} ipython3
294
- :tags: []
295
-
296
295
bootstrap.signatures
297
296
```
298
297
299
298
It shows that the function ` bootstrap ` takes one ` float64 ` floating point array, one function called ` mean ` and an ` int64 ` integer.
300
299
300
+ Now let's see what happens when we change the inputs.
301
+
301
302
Running it again with a larger integer for ` n ` and a different set of data does not change the signature of the function.
302
303
303
304
``` {code-cell} ipython3
@@ -308,27 +309,25 @@ bootstrap.signatures
308
309
309
310
As expected, the second run is much faster.
310
311
311
- Let's try to change the data again and use integer arrays as data
312
+ Let's try to change the data again and use an integer array as data
312
313
313
314
``` {code-cell} ipython3
314
- :tags: []
315
-
316
315
data = np.array([1, 2, 3, 4, 5], dtype=np.int64)
317
316
%time bootstrap(data, mean, 100)
318
317
bootstrap.signatures
319
318
```
320
319
321
- Note that a second signature is added into the signature of ` bootstrap ` with ` int64 ` array as the first argument .
320
+ Note that a second signature with an ` int64 ` array as the first argument is added into the signature of ` bootstrap ` function .
322
321
323
- The runtime is the same as if we ran the function for the first time.
322
+ The runtime is slower as if we ran the function for the first time.
324
323
325
- It suggests that Numba needs to recompile as the type changes.
324
+ It suggests that Numba recompiles this function as the type changes.
326
325
327
- Overall, type inference is what helps Numba to achieve its performance, but it also limits what Numba supports.
326
+ Overall, type inference helps Numba to achieve its performance, but it also limits what Numba supports as we have shown in the function example .
328
327
329
- You can refer to the list of supported Python and Numpy features [ here ] ( https://numba.pydata.org/numba-doc/dev/reference/pysupported.html ) .
328
+ In fact, this limitation means that Numba does not support everything in Python and scientific libraries we learned before .
330
329
331
- +++
330
+ You can refer to the list of supported Python and Numpy features [ here ] ( https://numba.pydata.org/numba-doc/dev/reference/pysupported.html ) .
332
331
333
332
## Compiling Classes
334
333
0 commit comments