Accelerating array computations with Numexpr
Numexpr is a package that improves upon a weakness of NumPy; the evaluation of complex array expressions is sometimes slow. The reason is that multiple temporary arrays are created for the intermediate steps, which is suboptimal with large arrays. Numexpr evaluates algebraic expressions involving arrays, parses them, compiles them, and finally executes them faster than NumPy.
This principle is somewhat similar to Numba, in that normal Python code is compiled dynamically by a JIT compiler. However, Numexpr only tackles algebraic array expressions rather than arbitrary Python code. We will see how that works in this recipe.
Getting ready
You will find the instructions to install Numexpr in the documentation available at http://github.com/pydata/numexpr.
How to do it…
- Let's import NumPy and Numexpr:
In [1]: import numpy as np import numexpr as ne
- Then, we generate three large vectors:
In [2]: x, y, z = np.random.rand(3, 1000000)
- Now...