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
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.
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.
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
35.955/166.227=21.63006%
About 5 times faster!
――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.
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.
――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