This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.

5.2. Accelerating array computations with Numexpr

Let's import NumPy and Numexpr.

In [ ]:
import numpy as np
import numexpr as ne

We generate three large vectors.

In [ ]:
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.

In [ ]:
%timeit x + (y**2 + (z*x + 1)*3)

And now, the same calculation performed by Numexpr. We need to give the formula as a string as Numexpr will parse it and compile it.

In [ ]:
%timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')

Numexpr also makes use of multicore processors. Here, we have 4 physical cores and 8 virtual threads with hyperthreading. We can specify how many cores we want numexpr to use.

In [ ]:
ne.ncores
In [ ]:
for i in range(1, 5):
    ne.set_num_threads(i)
    %timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).