Consider the following Pythonic snippet, "timing.py":
import numpy as np
def exponentials(n_, x_):
n_exp = np.arange(0,n_,1)
y_exp = np.zeros(n_)
y_exp[:] = np.exp(-x_*n_exp[:])
def powers(n_, x_):
n_exp = np.arange(0,n_,1)
y_exp = np.zeros(n_)
expbase = np.exp(-x_)
y_exp = expbase**n_exp
Now consider the following timing result
In [1]: import timing
In [2]: %timeit timing.exponentials(1e3, 1.1)
100000 loops, best of 3: 14 µs per loop
In [3]: %timeit timing.powers(1e3, 1.1)
10000 loops, best of 3: 27.5 µs per loop
I would have expected that evaluating the exponential function once, and then doing polynomials would have been significantly faster than evaluating the exponential function 10^3 times. At best, I expected them to be comparable. Does anybody have any insight in what's going on here?
This is NumPy version 1.9.2 with Python 2.7.10, if that matters.