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?


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:

              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




Should be official oversight of the operation, etc. Even np.exp(0)

equally slow and it doesn't have to compute anything.

>>> timeit.timeit("np.exp(100)", setup="import numpy as np")
>>> timeit.timeit("np.exp(0)", setup="import numpy as np")




All Articles