[PYTHON] Explicitly writing a loop in numpy is extremely slow

As mentioned in "How to use computer languages slowly" Writing a loop explicitly with numpy is extremely slow. The following is a story for beginners.

I wrote the code to experiment with it.

matmul.py


# -*- coding: utf-8 -*-
u"""
Matrix operation speed comparison
"""

def matmul1(a, b):
    lenI = a.shape[0]
    lenJ = a.shape[1]
    lenK = b.shape[1]
    c = np.zeros((lenI, lenJ))
    for i in range(lenI):
        for j in range(lenJ):
            for k in range(lenK):
                c[i, j] += a[i, k] * b[k, j]
    return c

def matmul2(a, b):
    lenI = a.shape[0]
    lenJ = a.shape[1]
    lenK = b.shape[1]
    c = np.zeros((lenI, lenJ))
    for i in range(lenI):
        for k in range(lenK):
            for j in range(lenJ):
                c[i, j] += a[i, k] * b[k, j]
    return c

import numpy as np
a = np.random.randn(200, 200)
b = np.random.randn(200, 200)
print a.shape

c2 = matmul2(a, b)
print c2

c = matmul1(a, b)
print c

c3 = np.dot(a, b)
print c3

In the IPython console Run this code once to enable the function definition I compared using% timeit. It makes a difference of 20 times.

%timeit c2 = matmul2(a, b) 1 loops, best of 3: 7.39 s per loop

%timeit c1 = matmul1(a, b) 1 loops, best of 3: 7.31 s per loop

%timeit c3 =np.dot(a, b) 1000 loops, best of 3: 321 µs per loop

Summary When using a matrix-optimized library like numpy Basically, do not write a for statement that accesses the elements of the matrix individually. Read the numpy documentation to find out which function to call. For Spyder integrated environment [help] [Installed Python Modules] You can read the numpy documentation.

See the power of speeding up with NumPy / SciPy

Related article

Do not write double loops in image processing as much as possible

Recommended Posts

Explicitly writing a loop in numpy is extremely slow
When writing a program in Python
Hash in Perl is a dictionary in Python
Is Python's in operator slow? (From ABC167D)
Python program is slow! I want to speed up! In such a case ...
A memo about writing merge sort in Python
Create a loop antenna pattern in Python in KiCad