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: doc/source/enhancingperf.rst
+20-14
Original file line number
Diff line number
Diff line change
@@ -311,7 +311,10 @@ Numba works by generating optimized machine code using the LLVM compiler infrast
311
311
312
312
As of ``numba`` version 0.20, pandas objects cannot be passed directly to numba-compiled functions. Instead, one must pass the ``numpy`` array underlying the ``pandas`` object to the numba-compiled function as demonstrated below.
313
313
314
-
We simply take the plain python code from above and annotate with the ``@jit`` decorator.
314
+
jit
315
+
~~~
316
+
317
+
Using ``numba`` to just-in-time compile your code. We simply take the plain python code from above and annotate with the ``@jit`` decorator.
315
318
316
319
.. code-block:: python
317
320
@@ -349,41 +352,44 @@ Note that we directly pass ``numpy`` arrays to the numba function. ``compute_num
349
352
In [4]: %timeit compute_numba(df)
350
353
1000 loops, best of 3: 798 us per loop
351
354
352
-
``numba`` can also be used to write vectorized functions that do not require the user to explicitly
353
-
loop over the observations of a vector; a vectorized function will be applied to each row automatically.
354
-
Consider the following toy example of doubling each observation:
355
+
vectorize
356
+
~~~~~~~~~
357
+
358
+
``numba`` can also be used to write vectorized functions that do not require the user to explicitly
359
+
loop over the observations of a vector; a vectorized function will be applied to each row automatically.
360
+
Consider the following toy example of doubling each observation:
355
361
356
362
.. code-block:: python
357
363
358
364
import numba
359
-
365
+
360
366
defdouble_every_value_nonumba(x):
361
367
return x*2
362
-
368
+
363
369
@numba.vectorize
364
370
defdouble_every_value_withnumba(x):
365
371
return x*2
366
-
367
-
368
-
# Custom function without numba
372
+
373
+
374
+
# Custom function without numba
369
375
In [5]: %timeit df['col1_doubled'] = df.a.apply(double_every_value_nonumba)
370
376
1000 loops, best of 3: 797 us per loop
371
-
377
+
372
378
# Standard implementation (faster than a custom function)
373
379
In [6]: %timeit df['col1_doubled'] = df.a*2
374
380
1000 loops, best of 3: 233 us per loop
375
-
381
+
376
382
# Custom function with numba
377
383
In [7]: %timeit df['col1_doubled'] = double_every_value_withnumba(df.a.values)
378
384
1000 loops, best of 3: 145 us per loop
379
385
380
386
.. note::
381
387
382
-
``numba`` will execute on any function, but can only accelerate certain classes of functions.
388
+
``numba`` will execute on any function, but can only accelerate certain classes of functions.
383
389
384
-
``numba`` is best at accelerating functions that apply numerical functions to numpy arrays. When passed a function that only uses operations it knows how to accelerate, it will execute in ``nopython`` mode.
390
+
``numba`` is best at accelerating functions that apply numerical functions to numpy arrays. When passed a function that only uses operations it knows how to accelerate, it will execute in ``nopython`` mode.
385
391
386
-
If ``numba`` is passed a function that includes something it doesn't know how to work with -- a category that currently includes sets, lists, dictionaries, or string functions -- it will revert to ``object mode``. In ``object mode``, numba will execute but your code will not speed up significantly. If you would prefer that ``numba`` throw an error if it cannot compile a function in a way that speeds up your code, pass numba the argument ``nopython=True`` (e.g. ``@numba.jit(nopython=True)``). For more on troubleshooting ``numba`` modes, see the `numba troubleshooting page <http://numba.pydata.org/numba-doc/0.20.0/user/troubleshoot.html#the-compiled-code-is-too-slow>`__.
392
+
If ``numba`` is passed a function that includes something it doesn't know how to work with -- a category that currently includes sets, lists, dictionaries, or string functions -- it will revert to ``object mode``. In ``object mode``, numba will execute but your code will not speed up significantly. If you would prefer that ``numba`` throw an error if it cannot compile a function in a way that speeds up your code, pass numba the argument ``nopython=True`` (e.g. ``@numba.jit(nopython=True)``). For more on troubleshooting ``numba`` modes, see the `numba troubleshooting page <http://numba.pydata.org/numba-doc/0.20.0/user/troubleshoot.html#the-compiled-code-is-too-slow>`__.
387
393
388
394
Read more in the `numba docs <http://numba.pydata.org/>`__.
0 commit comments