@@ -298,7 +298,7 @@ advanced Cython techniques:
298
298
299
299
Even faster, with the caveat that a bug in our Cython code (an off-by-one error,
300
300
for example) might cause a segfault because memory access isn't checked.
301
- For more about ``boundscheck `` and ``wraparound ``, see the Cython docs on
301
+ For more about ``boundscheck `` and ``wraparound ``, see the Cython docs on
302
302
`compiler directives <http://cython.readthedocs.io/en/latest/src/reference/compilation.html?highlight=wraparound#compiler-directives >`__.
303
303
304
304
.. _enhancingperf.numba :
@@ -323,7 +323,7 @@ Numba works by generating optimized machine code using the LLVM compiler infrast
323
323
Jit
324
324
~~~
325
325
326
- We demonstrate how to use Numba to just-in-time compile our code. We simply
326
+ We demonstrate how to use Numba to just-in-time compile our code. We simply
327
327
take the plain Python code from above and annotate with the ``@jit `` decorator.
328
328
329
329
.. code-block :: python
@@ -332,35 +332,38 @@ take the plain Python code from above and annotate with the ``@jit`` decorator.
332
332
333
333
@numba.jit
334
334
def f_plain (x ):
335
- return x * (x - 1 )
335
+ return x * (x - 1 )
336
+
336
337
337
338
@numba.jit
338
339
def integrate_f_numba (a , b , N ):
339
- s = 0
340
- dx = (b - a) / N
341
- for i in range (N):
342
- s += f_plain(a + i * dx)
343
- return s * dx
340
+ s = 0
341
+ dx = (b - a) / N
342
+ for i in range (N):
343
+ s += f_plain(a + i * dx)
344
+ return s * dx
345
+
344
346
345
347
@numba.jit
346
348
def apply_integrate_f_numba (col_a , col_b , col_N ):
347
- n = len (col_N)
348
- result = np.empty(n, dtype = ' float64' )
349
- assert len (col_a) == len (col_b) == n
350
- for i in range (n):
351
- result[i] = integrate_f_numba(col_a[i], col_b[i], col_N[i])
352
- return result
349
+ n = len (col_N)
350
+ result = np.empty(n, dtype = ' float64' )
351
+ assert len (col_a) == len (col_b) == n
352
+ for i in range (n):
353
+ result[i] = integrate_f_numba(col_a[i], col_b[i], col_N[i])
354
+ return result
355
+
353
356
354
357
def compute_numba (df ):
355
- result = apply_integrate_f_numba(df[' a' ].values, df[' b' ].values, df[' N' ].values)
356
- return pd.Series(result, index = df.index, name = ' result' )
358
+ result = apply_integrate_f_numba(df[' a' ].values, df[' b' ].values,
359
+ df[' N' ].values)
360
+ return pd.Series(result, index = df.index, name = ' result' )
357
361
358
- Note that we directly pass NumPy arrays to the Numba function. ``compute_numba `` is just a wrapper that provides a nicer interface by passing/returning pandas objects.
362
+ Note that we directly pass NumPy arrays to the Numba function. ``compute_numba `` is just a wrapper that provides a
363
+ nicer interface by passing/returning pandas objects.
359
364
360
- .. code-block :: ipython
361
-
362
- In [4]: %timeit compute_numba(df)
363
- 1000 loops, best of 3: 798 us per loop
365
+ >>> % timeit compute_numba(df)
366
+ 1000 loops, best of 3: 798 us per loop
364
367
365
368
In this example, using Numba was faster than Cython.
366
369
@@ -376,24 +379,25 @@ Consider the following toy example of doubling each observation:
376
379
import numba
377
380
378
381
def double_every_value_nonumba (x ):
379
- return x* 2
382
+ return x * 2
383
+
380
384
381
385
@numba.vectorize
382
386
def double_every_value_withnumba (x ):
383
- return x* 2
387
+ return x * 2
384
388
385
389
386
- # Custom function without numba
387
- In [ 5 ]: % timeit df[' col1_doubled' ] = df.a.apply(double_every_value_nonumba)
388
- 1000 loops, best of 3 : 797 us per loop
390
+ >>> # Custom function without numba
391
+ >>> % timeit df[' col1_doubled' ] = df.a.apply(double_every_value_nonumba)
392
+ 1000 loops, best of 3: 797 us per loop
389
393
390
- # Standard implementation (faster than a custom function)
391
- In [ 6 ]: % timeit df[' col1_doubled' ] = df.a* 2
392
- 1000 loops, best of 3 : 233 us per loop
394
+ >>> # Standard implementation (faster than a custom function)
395
+ >>> % timeit df[' col1_doubled' ] = df.a* 2
396
+ 1000 loops, best of 3: 233 us per loop
393
397
394
- # Custom function with numba
395
- In [ 7 ]: % timeit df[' col1_doubled' ] = double_every_value_withnumba(df.a.values)
396
- 1000 loops, best of 3 : 145 us per loop
398
+ >>> # Custom function with numba
399
+ >>> % timeit df[' col1_doubled' ] = double_every_value_withnumba(df.a.values)
400
+ 1000 loops, best of 3: 145 us per loop
397
401
398
402
Caveats
399
403
~~~~~~~
@@ -402,18 +406,18 @@ Caveats
402
406
403
407
Numba will execute on any function, but can only accelerate certain classes of functions.
404
408
405
- Numba is best at accelerating functions that apply numerical functions to NumPy
406
- arrays. When passed a function that only uses operations it knows how to
409
+ Numba is best at accelerating functions that apply numerical functions to NumPy
410
+ arrays. When passed a function that only uses operations it knows how to
407
411
accelerate, it will execute in ``nopython `` mode.
408
412
409
- If Numba is passed a function that includes something it doesn't know how to
410
- work with -- a category that currently includes sets, lists, dictionaries, or
411
- string functions -- it will revert to ``object mode ``. In ``object mode ``,
412
- Numba will execute but your code will not speed up significantly. If you would
413
- prefer that Numba throw an error if it cannot compile a function in a way that
414
- speeds up your code, pass Numba the argument
415
- ``nopython=True `` (e.g. ``@numba.jit(nopython=True) ``). For more on
416
- troubleshooting Numba modes, see the `Numba troubleshooting page
413
+ If Numba is passed a function that includes something it doesn't know how to
414
+ work with -- a category that currently includes sets, lists, dictionaries, or
415
+ string functions -- it will revert to ``object mode ``. In ``object mode ``,
416
+ Numba will execute but your code will not speed up significantly. If you would
417
+ prefer that Numba throw an error if it cannot compile a function in a way that
418
+ speeds up your code, pass Numba the argument
419
+ ``nopython=True `` (e.g. ``@numba.jit(nopython=True) ``). For more on
420
+ troubleshooting Numba modes, see the `Numba troubleshooting page
417
421
<http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#the-compiled-code-is-too-slow> `__.
418
422
419
423
Read more in the `Numba docs <http://numba.pydata.org/ >`__.
0 commit comments