What I did when I wanted to make Python faster -Numba edition-

Preface

http://qiita.com/open_cans/items/f180ae4dc945dc7b9066 This is a continuation of this article. I mainly write for people who care about milliseconds and microseconds.

What is numba

numba is a guy who compiles python code with a jit compiler to make it faster A pretty amazing guy who can speed up very easily

Example of using numba

sample.py


import numba

@numba.jit
def sumation(num):
  sum=0
  for i in range(num):
    sum += i
  return sum

sum = sumation(100)
print(sum)

Before the function as in the third line @numba.jit Just write It's amazing how easy it is to speed up! !!

numba notes

It's easy, but there are some things to keep in mind when using it, so I'll list them.

Inclusive notation cannot be used

In the previous article, I wrote that the inclusion notation is the best, but it can not be used with numba. I don't think it's there, but if you look at the previous article and write the comprehension, you have to rewrite the comprehension to the for statement.

Empty list cannot be used

sample.py


import numba

@numba.jit
def inputList(num):
  temp=[]
  temp.append(num)
  return sum

sum = inputList(100)
print(sum)

If you write like this, you will get an error Temp = [] is the cause

Precautions when compiling

If you write @ numba.jit in a function that doesn't work in the first place, the error will be hidden and you will not be able to understand it well, so please be sure to write @ numba.jit after confirming that it works reliably.

Precautions when combining numba and numpy

Sometimes I try to speed up a function that uses numpy with numba, but sometimes I can't. Apparently numba is not supported by some numpy functions This article (http://nekowarau.seesaa.net/article/428663212.html) According to numba, the supported numpy functions are (http://numba.pydata.org/numba-doc/0.14/numpy_support.html) Seems to be able to confirm with

If it doesn't, write the function in scratch Then you can get the benefit of speeding up.

Recommended Posts

What I did when I wanted to make Python faster -Numba edition-
What I did when updating from Python 2.6 to 2.7
What I did to save Python memory
[Python] What I did to do Unit Test
What I was addicted to when using Python tornado
What I did to welcome the Python2 EOL with confidence
What I was addicted to when migrating Processing users to Python
When I got a list of study sessions in Python, I found something I wanted to make
What I was addicted to when introducing ALE to Vim for Python
What I referred to when studying tkinter
I wanted to solve ABC160 with Python
I wanted to solve ABC159 in Python
What I did with a Python array
What I was addicted to Python autorun
I wanted to solve ABC172 with Python
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[At Coder] What I did to reach the green rank in Python
I refactored "I tried to make Othello AI when programming beginners studied python"
I wanted to solve NOMURA Contest 2020 with Python
I want to make a game with Python
I wanted to install Python 3.4.3 with Homebrew + pyenv
[Question] What happens when I use% in python?
I want to make C ++ code from Python code!
How to make Python faster for beginners [numpy]
What I did when I got stuck in the time limit with lambda python
What I do when imitating embedded go in python
When I tried to introduce python3 to atom, I got stuck
Continuation ・ I tried to make Slackbot after studying Python3
What I did when I was angry to put it in with the enable-shared option
What to do when "cannot import name xxx" [Python]
What I did to ssh to the VPS Ubuntu environment
What I did to get started with Linux commands
I want to do something in Python when I finish
What to do when you can't bind CaboCha to Python
What I did to speed up the string search task
I wanted to solve the Panasonic Programming Contest 2020 with Python
I tried to make various "dummy data" with Python faker
I wanted to skip certain extensions when building Sphinx documentation
When I try to make Apache SSL, it doesn't start.
What to do when "SSL: CERTIFICATE_VERIFY_FAILED _ssl.c: 1056" appears in Python
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I want to make input () a nice complement in python
[Python3] List of sites that I referred to when I started Python
What I learned in Python
Mayungo's Python Learning Episode 4: I tried to see what happens when numbers are treated as letters
What I did when I migrated to Linux 2 with EOL support for Amazon Linux (learning and failure stories)
[5th] I tried to make a certain authenticator-like tool with python
I tried to solve the ant book beginner's edition with python
What I did to output application logs on GAE Flexible Environment.
A note on what you did to use Flycheck with Python
[2nd] I tried to make a certain authenticator-like tool with python
I tried to make a regular expression of "amount" using Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
What I was addicted to with json.dumps in Python base64 encoding
[3rd] I tried to make a certain authenticator-like tool with python
I want to run the Python GUI when starting Raspberry Pi
I tried to make a regular expression of "date" using Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python