Jupyter Snippet CB2nd 03_numexpr

Jupyter Snippet CB2nd 03_numexpr

5.3. Accelerating array computations with Numexpr

import numpy as np
import numexpr as ne
x, y, z = np.random.rand(3, 1000000)
%timeit x + (y**2 + (z*x + 1)*3)
6.94 ms ± 223 µs per loop (mean ± std. dev. of 7 runs,
    100 loops each)
%timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)')
1.47 ms ± 8.07 µs per loop (mean ± std. dev. of 7 runs,
    1000 loops each)
ne.ncores
8
for i in range(1, 5):
    ne.set_num_threads(i)
    %timeit ne.evaluate('x + (y**2 + (z*x + 1)*3)',)
3.53 ms ± 12.9 µs per loop (mean ± std. dev. of 7 runs,
    100 loops each)
2.35 ms ± 276 µs per loop (mean ± std. dev. of 7 runs,
    100 loops each)
1.6 ms ± 60 µs per loop (mean ± std. dev. of 7 runs,
    1000 loops each)
1.5 ms ± 24.6 µs per loop (mean ± std. dev. of 7 runs,
    1000 loops each)