Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

np.exp 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 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?

like image 587
James Avatar asked May 04 '15 01:05

James


People also ask

Does NP do exp?

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}.

How do you write an exponential function in Numpy?

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.


1 Answers

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
like image 175
Marius Avatar answered Sep 19 '22 14:09

Marius