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 any reason that using the CPython implementation of np.e**100 is that much slower than using the numpy version? Shouldn't the numpy version be faster as it is pushed down to C code?
1 Answer. Show activity on this post. Any problem in NP is in EXPTIME because you can either use exponential time to try all possible certificates or to enumerate all possible computation paths of a nondeterministic machine. L={x∣(x,y)∈R}.
exp() in Python. numpy. exp(array, out = None, where = True, casting = 'same_kind', order = 'K', dtype = None) : This mathematical function helps user to calculate exponential of all the elements in the input array.
One obvious reason is that np.exp
is set up to handle arrays, and there's probably a bit of overhead involved there in terms of figuring out the type/dimensions of the input. Try out cases like these, and you might see the difference reduce or vanish:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With