Np.exp is much slower than np.e?
In [49]: timeit.timeit("np.exp(100)", setup="import numpy as np")
Out[49]: 1.700455904006958
In [50]: timeit.timeit("np.e**100", setup="import numpy as np")
Out[50]: 0.16629505157470703
Is there a reason why using the CPython implementation of np.e ** 100 is much slower than using the numpy version? Should the numpy version be faster as it is pushed to C code?
+3
source to share
2 answers
One obvious reason is that it is np.exp
configured to handle arrays and there is probably a little overhead to figure out the type / sizes of the input. Try cases like this and you can see the difference decrease or disappear:
timeit.timeit("np.exp(x)",
setup="import numpy as np; x = np.array([99, 100, 101])")
# This actually seems to be faster than just calculating
# it for a single value
Out[7]: 1.0747020244598389
timeit.timeit("[np.e**n for n in x]",
setup="import numpy as np; x = [99, 100, 101]")
Out[8]: 0.7991611957550049
+3
source to share