I am using n-body to compare the performance among Rust, PyO3 and Python.
This repo includes three parts of code:
rnbody
is Rust version, source code from benchmarksgame Rust .rnbody_pyo3
is python using pyo3 version, the main functionadvance
is using rust code to calculate.pnbody.py
is python version, source code from benchmarksgame Python with slightly update onBODIES
data type.
rnbody
: Make sure installedrust
andcargo
. And then runcargo run 10000
inrbody
folder. (10000
is steps which used in calculating nbody)rnbody_pyo3
: - Install rust nightly. Currentlymaster
branch of PyO3 is able to use stable rust version. But I am using0.10.1
of pyo3 (Reuqired Python3). - Install setuptools-rust . - In folderrnbody_pyo3
runpython setup.py develop
. - Run./pnbody_pyo3 10000
(usingPlanet
struct to representing planet data) or./pnbody_pyo3 10000 --list
(usingvec
to representing planet data)pnbody.py
: Run./pnbody.py 10000
.
Testing setup:
- PC: ThinkPad T490
- CPU: intel i7 8th Gen
- OS: Ubuntu 16.04
- Python version: 3.7.6
- rustc version: 1.46.0-nightly (
ff5b446d2
2020-06-23) - cargo version: 1.46.0-nightly (
089cbb80b
2020-06-15) - PyO3 version: 0.10.1
setuptools-rust
: 0.10.6
Run following commands 100 times each with different steps value.
(Used steps: 10K, 100K, 1M, 10M)
- Python
./pnbody.py <steps>
- PyO3 with
list
struct./pnbody_pyo3 <steps> --list
inrnbody_pyo3
folder. - PyO3 with rust
Planet
struct./pnbody_pyo3 <steps>
inrnbody_pyo3
folder. - Rust
cargo run <steps>
inrnbody
folder.
Performance chart:
After running each combination over 100 times on my computer, the below table shows the results.
steps | 10,000 | 100,000 | 1,000,000 | 10,000,000 |
---|---|---|---|---|
python3 | 0.1068 | 1.0352 | 11.3587 | 108.8791 |
pyo3 (list) | 0.0616 | 0.5126 | 5.4035 | 54.7067 |
pyo3 (planet struct) | 0.0215 | 0.0952 | 0.8882 | 9.5147 |
rust | 0.0009 | 0.0714 | 0.8708 | 9.204 |
each value is the average value after running 100 times. Unit: second |
Split chart into two charts since 10K and 10M have quite large scale differences.