Peter
Peter

Reputation: 377

Understanding run time with numpy arrays?

I was wondering if someone could help me understand why the following two programs run at significantly different speeds (first one takes like 1/10 of a second, second one takes like 3 seconds).

def gaussian(x, h, m, c):
    return list(h * exp(-(x-m)**2/(2*c**2)))

x = np.linspace(0, 1000, 1001)

for i in range(1000):
    gaussian(x, 50, 500, 10)

and

def gaussian2(x, h, m, c):
    def computer(x):
        return h * exp(-(x-m)**2/(2*c**2))
    y = []
    for val in x:
        y.append(computer(val))
return y

x = np.linspace(0, 1000, 1001)

for i in range(1000):
    gaussian2(x, 50, 500, 10)

The only reason I'd need the second version is because it let me look at all the elements in the list so I can perform other operations with them (not illustrated in this example). But that isn't what I am asking -- I am asking why the second form is so much slower than the first.

Thanks!

Upvotes: 1

Views: 833

Answers (1)

Vince W.
Vince W.

Reputation: 3785

MaxU is right that the main reason is the vectorized math in numpy is faster than the scalar math in Python. Its also important to remember however that looping over a numpy array is a performance hit compared to looping over a Python list. It doesn't show up as much as the math in this case, but there are other cases where it can be the major slowdown factor

import numpy as np
import math

def gaussian(x, h, m, c):
    return list(h * np.exp(-(x-m)**2/(2*c**2)))

def gaussian2(x, h, m, c):
    def computer(x):
        return h * math.exp(-(x-m)**2/(2*c**2))
    y = []
    for val in x:
        y.append(computer(val))
    return y

x = np.linspace(0, 1000, 1001)
x_list = x.tolist()

%timeit gaussian(x, 50, 500, 10)
%timeit gaussian2(x, 50, 500, 10)
%timeit gaussian2(x_list, 50, 500, 10)

yields:

10000 loops, best of 3: 114 µs per loop
100 loops, best of 3: 2.97 ms per loop
100 loops, best of 3: 2.35 ms per loop

so its clear the biggest bottleneck is the math, but there is a little slowdown from looping over the numpy array as compared with the list.

Upvotes: 1

Related Questions