I thought it would be slow to use a for statement in NumPy, but that wasn't the case.

Inspired by Explicitly writing a loop in numpy is extremely slow

In the above article, it was said that writing for explicitly would be extremely slow. For example

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

Code like is slower than np.dot.

%timeit matmul1(a, b)
1 loops, best of 3: 12.9 s per loop
%timeit np.dot(a, b)
10 loops, best of 3: 20.7 ms per loop

It's slow because it's calculated on my laptop. Also, atlas / mkl is not linked.

Now use Numba.

import numba

@numba.jit  #Add only here
def matmul1_jit(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

It JIT compiles Python code using LLVM, so it can run very fast. The first call includes time to compile, so if you measure the speed on subsequent calls:

%timeit matmul1_jit(a, b)
10 loops, best of 3: 24.4 ms per loop

Just adding one line like this made it about the same as np.dot (about 20% slower).

Put the whole ipynb in gist. I wish I could embed nbviewer in Qiita.

Recommended Posts

I thought it would be slow to use a for statement in NumPy, but that wasn't the case.
For the first time in Numpy, I will update it from time to time
Convenient to use matplotlib subplots in a for statement
I didn't know how to use the [python] for statement
It may be a problem to use Japanese for folder names and notebook names in Databricks
I want to specify a file that is not a character string for logrotate, but is it impossible?
I want to be healed by Mia Nanasawa's image. In such a case, hit the Twitter API ♪
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
Although I knew that the machine learning course in the example was good, I continued to go through it for two years, but it was still good
I tried using eval (a, b) for Fibonacci, but it wasn't fast
I wanted to use the find module of Ansible2, but it took some time, so make a note
How to save the feature point information of an image in a file and use it for matching
A solution to the problem that the Python version in Conda cannot be changed
I tried to predict the horses that will be in the top 3 with LightGBM
I searched for the skills needed to become a web engineer in Python
Python program is slow! I want to speed up! In such a case ...
[Python] I want to use only index when looping a list with a for statement
I tried to scrape YouTube, but I can use the API, so don't do it.
I will try to summarize the links that seem to be useful for the time being
It would be wise to write like boolean and "A" or "B" [Python] [But]
I tried to make a memo app that can be pomodoro, but a reflection record
I realized that it is nonsense to use the module without thinking because it is convenient.
As for the process type written in honcho's Procfile, I was addicted to it for over an hour because I couldn't use-, so I'll write it as a show.
When you want to plt.save in a for statement
I want to use the R dataset in python
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
Is it a problem to eliminate the need for analog human resources in the AI era?
[Part 1] I tried to solve the error "User Warning: An input could not be retrieved. It could be because a worker has died" that occurred in Mask R-CNN.
[Part 2] I tried to solve the error "User Warning: An input could not be retrieved. It could be because a worker has died" that occurred in Mask R-CNN.
I want to absorb the difference between the for statement on the Python + numpy matrix and the Julia for statement
I made a function to crop the image of python openCV, so please use it.
I made a tool that makes it convenient to set parameters for machine learning models.
How to use the __call__ method in a Python class
Use MeCab to translate sloppy sentences in a "slow" way.
How to define multiple variables in a python for statement
I wrote it in Go to understand the SOLID principle
I want to create a Dockerfile for the time being.
I wrote a script that splits the image in two
I tried to make a site that makes it easy to see the update information of Azure
I tried to expand the database so that it can be used with PES analysis software
I heard rumors that malloc is slow and should be stored in memory, so I compared it.
I made a function to check if the webhook is received in Lambda for the time being
A story that didn't work when I tried to log in with the Python requests module
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
I want to use a wildcard that I want to shell with Python remove
Let's do it by dividing numpy without using the for statement
I made a program that solves the spot the difference in seconds
[Selenium] Use a while statement to repeatedly move to the "next page"
A timer (ticker) that can be used in the field (can be used anywhere)
I want to create a pipfile and reflect it in docker
Let's do it by multiplying numpy without using the for statement
I made a command to display a colorful calendar in the terminal
I wrote a tri-tree that can be used for high-speed dictionary implementation in D language and Python.
[For beginners] I want to get the index of an element that satisfies a certain conditional expression
I tried to make a system to automatically acquire the program guide → register it in the calendar in one day
Solution to the problem that Ctrl + z cannot be used in Powershell in Docker for windows environment (provisional)
It seems that some RHEL will be free with a big boo for the end of CentOS
For the time being using FastAPI, I want to display how to use API like that on swagger
Create a bot that posts the number of people positive for the new coronavirus in Tokyo to Slack
I made an app for foreign visitors to Japan with a hackathon and won a prize, but when I thought about it carefully, it was useless.