Don't write Python if you want to speed it up with Python

Introduction

I'm going to have a lightning talk about Python at work, so I'll write something that can be read in about 5 minutes. The talk theme is "Knowledge for trying to speed up with Python". ** This is not an article that dissipates Python. ** **

Who is the target of this article

This article is intended for:

--Python beginners --People who are interested in Python but think that it is not speedy

Immediate conclusion

As the title says, if you want to speed up with Python, don't write Python (as much as possible). The purpose of this article is to reduce the amount of Python written and increase the processing speed.

Python is slow in the first place

Now, immediately, ** Python is a slow language ** compared to C ++, Java, etc. The slowness of Python is mentioned in various articles such as:

-Why is Python so slow? | POSTD -Is the Python for statement slow? --atsuoishimoto's diary -Speed comparison of Python, Java, C ++ --Qiita

In summary, Python is considered slow, mainly for the following reasons:

--Because it is an interpreted (non-compiled) language --Because it is a dynamically typed language

However, commonly considered heavy tasks such as machine learning and image processing are actively performed using Python. Why are these processes running at a speed that is practical? That's because ** most of the processing isn't written using Python **.

What do you mean

In other words, if you want to speed up Python, you can leave the heavy processing to standard functions and libraries written in C ++ instead of doing it in Python. See the two functions below. fastCode () is a function that can be executed fast. slowCode () is a slow function.

import numpy as np

#Fast code
def fastCode():
    #List is declared in list notation
    list1 = [ i for i in range(0,10000)]
    list2 = [ i for i in range(-10000,0)]

    #Use standard functions whenever possible
    listSum = sum(list1)

    #Use numpy to make a large amount of calculations into a determinant
    array1 = np.array(list1)
    array2 = np.array(list2)
    #Add all the elements of array1 and array2
    # [0, 1, 2, ...] + [-100000, -99999, -99998, ...]
    # >> [-100000, -99998, -99996, ...]
    array3 = array1 + array2
    #Multiply all elements of array1 and array2
    # [0, 1, 2, ...] * [-100000, -99999, -99998, ...]
    # >> [0, -99999, -199996, ...]
    array4 = array1 * array2

#Slow code
def slowCode():
    #Create a list using append
    list1 = []
    for i in range(0,10000):
        list1.append(i)
    list2 = []
    for i in range(-10000,0):
        list2.append(i)

    #Calculate the total by turning with a for statement without using standard functions
    listSum = 0
    for value in list1:
        listSum += value

    #Do a lot of calculations with a for statement
    list3 = []
    list4 = []
    for i , value in enumerate(list1):
        list3.append(value + list2[i])
        list4.append(value * list2[i])

You can see that fastCode () entrusts the processing to standard functions and libraries and can be coded short, while slowCode () describes the processing by itself using the for statement. I think you can. The actual processing speeds are as follows.

function Number of lines excluding comments Execution speed(Average of 100 runs)
fastCode() 7 lines 0.00207[sec]
slowCode() 14 lines 0.00430[sec]

Standard functions and libraries are composed of ** compiled binaries ** written in C / C ++, and by letting them handle the processing, even heavy tasks can be processed at high speed. From a "speedup" perspective, Python is, so to speak, a ** language for easy use of fast libraries **.

Summary

If you want to speed up the process in Python, use the following techniques to minimize the process written in Python.

--Use standard functions and libraries written in high-speed C / C ++ --A large amount of calculations are put into a determinant and processed at once with numpy --If you can't help, create your own library using C / C ++

Finally

There are many ways to speed up Python other than those mentioned here. However, I think this is the first thing to be aware of when accelerating with Python. When you need to find more speed, let's start with your own library, function pre-reference, Cython, Numba, etc.

reference

Why is Python so slow? | POSTD Is the Python for statement slow? --atsuoishimoto's diary Python, Java, C ++ speed comparison-Qiita [Introduction to Python C API] Create an extension module in C / C ++ and call it from Python -Part 1- Python Tips for Acceleration-A Leisure Engineer's Diary

Recommended Posts

Don't write Python if you want to speed it up with Python
Indispensable if you use Python! How to use Numpy to speed up operations!
[TensorFlow] If you want to run TensorBoard, install it with pip
I want to write to a file with Python
If you want to include awsebcli with CircleCI, specify the python version
Two document generation tools that you definitely want to use if you write python
How to write environment variables that you don't want to put on [GitHub] Python
[OpenCV] When you want to check if it is read properly with imread
Write to csv with Python
For those who want to write Python with vim
If you want to make a discord bot with python, let's use a framework
If you don't know how to draw the graph you want with matplotlib, it is convenient to look at the gallery.
Solution when you want to use cv_bridge with python3 (virtualenv)
[Python] If you suddenly want to create an inquiry form
Wrap C/C ++ with SWIG to speed up Python processing. [Overview]
If you write TinderBot in Python, she can do it
Numba to speed up as Python
[Python] Write to csv file with Python
Nice to meet you with python
Roughly speed up Python with numba
How to speed up Python calculations
I want to debug with Python
If you want to use field names with hyphens when updating firestore data in python
If you want to become a data scientist, start with Kaggle
What to do if you can't install pyaudio with pip #Python
I want to know if you install Python on Mac ・ Iroha
If you want to assign csv export to a variable in python
I want to analyze logs with Python
I want to play with aws with python
If you write go table driven test in python, it may be better to use subTest
I want to do it with Python lambda Django, but I will stop
I want to tweet on Twitter with Python, but I'm addicted to it
What to do if you couldn't send an email to Yahoo with Python.
When you want to use it as it is when using it with lambda memo
What to do if ipython and python start up with different versions
18 beautiful Python terms you want to read aloud. R18 with example sentences
[Python] If you want to draw a scatter plot of multiple clusters
If you want to get multiple statistics with groupby in pandas v1
If you want to count words in Python, it's convenient to use Counter.
QR code creation with Python. I don't want to line up because I write the same thing every time I issue a commuter pass. ..
I want to use MATLAB feval with python
I want to make a game with Python
If you want to create a Word Cloud.
[Write to map with plotly] Dynamic visualization with plotly [python]
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
Write CSV data to AWS-S3 with AWS-Lambda + Python
[Python] Do your best to speed up SQLAlchemy
AssertNumQueries is useful if you want to easily test N + 1 queries with django
If you want to make a TODO application (distributed) now using only Python
I want to write an element to a file with numpy and check it.
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
What you want to memorize with the basic "string manipulation" grammar of python
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
What to do if you get an error when installing python with pyenv
Python program is slow! I want to speed up! In such a case ...
If you want to enter the virtual environment with jupyter, nb_conda_kernels is recommended
If you try to install Python2 pip after installing Python3 pip and it is rejected
I want to write in Python! (1) Code format check