Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancescAlted/203be8a44d02566f31dae11a22c179f3 to your computer and use it in GitHub Desktop.
Save FrancescAlted/203be8a44d02566f31dae11a22c179f3 to your computer and use it in GitHub Desktop.
NumPy vs numexpr vs numba
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import numpy as np
In [2]: import numexpr as ne
In [3]: import numba
In [4]: x = np.linspace(0, 10, int(1e8))
In [5]: newfactor = .1
In [6]: %time y = np.sin(x) * np.exp(newfactor * x)
CPU times: user 824 ms, sys: 1.21 s, total: 2.03 s
Wall time: 2.03 s
In [7]: %time y = ne.evaluate("sin(x) * exp(newfactor * x)")
CPU times: user 4.4 s, sys: 696 ms, total: 5.1 s
Wall time: 170 ms
In [8]: ne.set_num_threads(16) # kind of optimal for this machine
Out[8]: 56
In [9]: %time y = ne.evaluate("sin(x) * exp(newfactor * x)")
CPU times: user 888 ms, sys: 564 ms, total: 1.45 s
Wall time: 120 ms
In [10]: @numba.jit(nopython=True, cache=True, fastmath=True)
...: def expr_numba(x, newfactor):
...: y = np.empty(x.shape, x.dtype)
...: for i in range(len(x)):
...: y[i] = np.sin(x[i]) * np.exp(newfactor * x[i])
...: return y
...:
In [11]: %time y = expr_numba(x, newfactor)
CPU times: user 6.68 s, sys: 460 ms, total: 7.14 s
Wall time: 7.13 s
In [12]: @numba.jit(nopython=True, cache=True, fastmath=True, parallel=True)
...: def expr_numba(x, newfactor):
...: y = np.empty(x.shape, x.dtype)
...: for i in range(len(x)):
...: y[i] = np.sin(x[i]) * np.exp(newfactor * x[i])
...: return y
...:
In [13]: %time y = expr_numba(x, newfactor)
/root/miniconda3/lib/python3.7/site-packages/numba/compiler.py:602: NumbaPerformanceWarning:
The keyword argument 'parallel=True' was specified but no transformation for parallel execution was possible.
To find out why, try turning on parallel diagnostics, see http://numba.pydata.org/numba-doc/latest/user/parallel.html#diagnostics for help.
File "<ipython-input-12-23ff37f79862>", line 2:
@numba.jit(nopython=True, cache=True, fastmath=True, parallel=True)
def expr_numba(x, newfactor):
^
self.func_ir.loc))
CPU times: user 6.62 s, sys: 468 ms, total: 7.09 s
Wall time: 7.08 s
In [14]: np.__version__
Out[14]: '1.16.4'
In [15]: ne.__version__
Out[15]: '2.6.9'
In [16]: numba.__version__
Out[16]: '0.45.0'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment