A primer to vector computing

Multidimensional arrays

The ndarray

Vector operations on ndarrays

Example: how fast are vector computations in NumPy?


In [1]:
from random import random
list_1 = [random() for _ in range(1000000)]
list_2 = [random() for _ in range(1000000)]

In [2]:
out = [x + y for (x, y) in zip(list_1, list_2)]
out[:3]


Out[2]:
[0.843375384328939, 1.507485612134079, 1.4119777108063973]

In [3]:
%timeit [x + y for (x, y) in zip(list_1, list_2)]


Out[3]:
10 loops, best of 3: 69.7 ms per loop

In [4]:
import numpy as np
arr_1 = np.array(list_1)
arr_2 = np.array(list_2)

In [5]:
type(list_1), type(arr_1)


Out[5]:
(list, numpy.ndarray)

In [6]:
arr_1.shape


Out[6]:
(1000000,)

In [7]:
arr_1.dtype


Out[7]:
dtype('float64')

In [8]:
sum_arr = arr_1 + arr_2
sum_arr[:3]


Out[8]:
array([ 0.84337538,  1.50748561,  1.41197771])

In [9]:
%timeit arr_1 + arr_2


Out[9]:
1000 loops, best of 3: 1.57 ms per loop

How an ndarray is stored in memory

Why operations on ndarrays are fast