Skip to content

Numba #
Find similar titles

Structured data

Category
Programming
Big Data

Numba gives you the power to speed up your applications with high performance functions written directly in Python. With a few annotations, array-oriented and math-heavy Python code can be just-in-time compiled to native machine instructions, similar in performance to C, C++ and Fortran, without having to switch languages or Python interpreters.

Numba works by generating optimized machine code using the LLVM compiler infrastructure at import time, runtime, or statically (using the included pycc tool). Numba supports compilation of Python to run on either CPU or GPU hardware, and is designed to integrate with the Python scientific software stack.

Install #

$ virtualenv --python=python3.4 --always-copy numba-env
$ source numba-env/bin/activate
$ pip install numba

Example #

#!/usr/bin/env python3
from numba import jit
from numpy import arange
import cProfile


##@jit
def sum2d(arr):
    M, N = arr.shape
    result = 0.0
    for i in range(M):
        for j in range(N):
            result += arr[i,j]
    return result

a = arange(3000*3000).reshape(3000, 3000)

cProfile.run('sum2d(a)')

Conclusion #

Without @jit #

$ ./run.py
     4 function calls in 2.482 seconds
Ordered by: standard name
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    1    0.000    0.000    2.482    2.482 <string>:1(<module>)
    1    2.482    2.482    2.482    2.482 run.py:8(sum2d)
    1    0.000    0.000    2.482    2.482 {built-in method exec}
    1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

With @jit #

$ ./run.py
     76244 function calls (69807 primitive calls) in 0.105 seconds
Ordered by: standard name
ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    2    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap>:2264(_handle_fromlist)
    1    0.017    0.017    0.105    0.105 <string>:1(<module>)
    1    0.000    0.000    0.000    0.000 <string>:12(__new__)
    1    0.000    0.000    0.000    0.000 __init__.py:41(__init__)
    2    0.000    0.000    0.000    0.000 __init__.py:47(create_string_buffer)
    4    0.000    0.000    0.000    0.000 __init__.py:487(cast)
    ......

Documentation #

  1. http://numba.pydata.org/

References #

  1. http://jakevdp.github.io/blog/2013/06/15/numba-vs-cython-take-2/
  2. http://continuum.io/blog/numba_growcut
  3. http://continuum.io/blog/numba_performance
  4. http://numba.pydata.org/
  5. http://nbviewer.ipython.org/urls/raw.github.com/aterrel/HPCPythonSC2012/master/02_Speeding_Python.ipynb

Suggested Pages #

0.0.1_20140628_0