Accelerating array computations with NumExpr
NumExpr is a package that can offer some speedup on complex computations on NumPy arrays. NumExpr evaluates algebraic expressions involving arrays, parses them, compiles them, and finally executes them, possibly on multiple processors.
This principle is somewhat similar to Numba, in that normal Python code is compiled dynamically to machine code. However, NumExpr only tackles algebraic array expressions rather than arbitrary Python code. We will see how that works in this recipe.
Getting ready
NumExpr should already be installed in Anaconda, but you can also install it manually with conda install numexpr
.
How to do it...
- Let's import NumPy and NumExpr:
>>> import numpy as np import numexpr as ne
- Then we generate three large vectors:
>>> x, y, z = np.random.rand(3, 1000000)
- Now, we evaluate the time taken by NumPy to calculate a complex algebraic expression involving our vectors:
>>> %timeit x + (y**2 + (z*x + 1)*3)...