numpy ufuncs speed vs for loop speed
I've read a lot "avoid for loops with numpy". So, I tried. I was using this code (simplified version). Some auxiliary data: In[1]: import numpy as np resolution = 1000 # this parameter varies tim = np.linspace(-np.pi, np.pi, resolution) prec = np.arange(1, resolution + 1) prec = 2 * prec - 1 values = np.zeros_like(tim) My first implementation was with for loop: In[2]: for i, ti in enumerate(tim): values[i] = np.sum(np.sin(prec * ti)) Then, I got rid of the explicit for cycle, and achieved this: In[3]: values = np.sum(np.sin(tim[:, np.newaxis] * prec), axis=1) And this solution was faster for