I replaced the numerical calculation of Python with Rust and compared the speed

TL;DR

Numerical calculation that took 3 minutes in Python ・ When I replaced the text file writing process of about 8 million lines with Rust, it became about 5 times faster

Introduction

https://github.com/MIERUNE/japan-mesh-tool

The above is a Python module that generates a Japanese regional mesh in .geojsonl format, which was also introduced in this article. It is a pure numerical calculation that can be written only with the standard module, but for example, with a cubic mesh, the number of polygons is less than 8 million, and as will be described later, the time required is about 3 minutes.

I also tried to study Rust to see how much faster it would be if I replaced this Python process with Rust. By the way, the Rust code is also included in the above repository.

Reference: Consider speeding up on Python

Playing with the algorithm did not result in a dramatic speed improvement. Python itself is an interpreted language and you shouldn't ask for speed. Also, if you use numpy, it will be faster with the help of C language, but I dropped it because I wanted to keep it independent of external modules.

Python vs Rust speed comparison

test conditions

Fight!

Python

time python main.py 3
initializing...
making meshes...
writing file...
done

real    2m46.227s
user    2m31.846s
sys     0m11.249s

166.227 seconds

Rust

time ./target/release/japan-mesh 3

real    0m35.955s
user    0m34.233s
sys     0m1.272s

35.955 seconds

result

35.955/166.227=21.63006%

About 5 times faster!

About Rust

Why Rust?

――It seems to be fast ――It seems to be hot now

I started with such a light understanding. When I think about it now, I think the most attractive thing is the ease of building the environment. You can enter the compiler, package manager, etc. with a single command.

Is Rust difficult?

It's a bit bitter, but it's more difficult than Python. However, what makes Rust difficult is the concept of ownership system and lifetime, and with just numerical calculations like this one, you can make something that works even if the compiler gets angry.

Impressions

――I'm glad that Rust, which should have a strange code, is fast. ――Of course, the speed difference of the calculation part is obvious, but it seems that the file writing will not change so much. --Rust gets angry when the compiler writes good and weird code ――I think there is room for optimization in the code. --Initially, I used georust / geojson, but to_string () was very slow.

Recommended Posts

I replaced the numerical calculation of Python with Rust and compared the speed
I compared the speed of Hash with Topaz, Ruby and Python
I measured the speed of list comprehension, for and while with python2.7.
I compared the speed of go language web framework echo and python web framework flask
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
I compared the speed of the reference of the python in list and the reference of the dictionary comprehension made from the in list.
I tried to compare the processing speed with dplyr of R and pandas of Python
I compared the calculation time of the moving average written in Python
[Introduction to Python] I compared the naming conventions of C # and Python.
Compare the speed of Python append and map
Numerical calculation with Python
I compared the moving average of IIR filter type with pandas and scipy
Visualize the range of interpolation and extrapolation with python
I checked out the versions of Blender and Python
Start numerical calculation in Python (with Homebrew and pip)
I compared Java and Python!
I tried to automate the article update of Livedoor blog with Python and selenium.
I replaced the Windows PowerShell cookbook with a python script.
I tried "gamma correction" of the image with Python + OpenCV
[Python] Heron's formula functionalization and calculation of the maximum area
I wrote the basic grammar of Python with Jupyter Lab
I evaluated the strategy of stock system trading with Python.
I want to know the features of Python and pip
Play with the password mechanism of GitHub Webhook and Python
The story of Python and the story of NaN
Play with numerical calculation of magnetohydrodynamics
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I liked the tweet with python. ..
I played with PyQt5 and Python3
Coexistence of Python2 and 3 with CircleCI (1.0)
I tried scraping the ranking of Qiita Advent Calendar with Python
Speed comparison of Wiktionary full text processing with F # and Python
I want to output the beginning of the next month with Python
[Python] I thoroughly explained the theory and implementation of logistic regression
[Python] I thoroughly explained the theory and implementation of decision trees
I tried to improve the efficiency of daily work with Python
I compared python3 standard argparse and python-fire
Check the existence of the file with python
I didn't know the basics of Python
Numerical calculation of differential equations with TensorFlow 2.0
I installed and used Numba with Python3.5
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
The Python project template I think of.
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
I set the environment variable with Docker and displayed it in Python
I tried to get the authentication code of Qiita API with Python.
I vectorized the chord of the song with word2vec and visualized it with t-SNE
I have 0 years of programming experience and challenge data processing with python
Find the general terms of the Tribonacci sequence with linear algebra and Python
I tried to verify and analyze the acceleration of Python by Cython
Get the number of articles accessed and likes with Qiita API + Python
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
Get and estimate the shape of the head using Dlib and OpenCV with python
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
[Python] I introduced Word2Vec and played with it.
Prepare the execution environment of Python3 with Docker
Summary of the differences between PHP and Python
2016 The University of Tokyo Mathematics Solved with Python