CGO 02-2: Timing Numpy

Jupyter Notebookのファイルをここからダウンロードしてください。

CGO 02-2: Timing Numpy

This notebook shows off how we can make code significantly faster by using vector operations provided by numpy.

import numpy as np
np.random.seed(42)
def compute_reciprocals(values):
    output = np.empty(len(values))
    for i in range(len(values)):
        output[i] = 1.0 / values[i]
    return output
big_array = np.random.randint(1, 100, size=1000000)
%timeit compute_reciprocals(big_array)
%timeit (1.0 / big_array)