/nbody_rs_pyo3_py

Rust, Pyo3, Python Performance Comparison

Primary LanguagePython

Performance Comparison: Rust vs PyO3 vs Python

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 function advance is using rust code to calculate.
  • pnbody.py is python version, source code from benchmarksgame Python with slightly update on BODIES data type.

How to Use

  • rnbody: Make sure installed rust and cargo. And then run cargo run 10000 in rbody folder. (10000 is steps which used in calculating nbody)
  • rnbody_pyo3: - Install rust nightly. Currently master branch of PyO3 is able to use stable rust version. But I am using 0.10.1 of pyo3 (Reuqired Python3). - Install setuptools-rust . - In folder rnbody_pyo3 run python setup.py develop. - Run ./pnbody_pyo3 10000 (using Planet struct to representing planet data) or ./pnbody_pyo3 10000 --list (using vec to representing planet data)
  • pnbody.py: Run ./pnbody.py 10000.

Performance

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 in rnbody_pyo3 folder.
  • PyO3 with rust Planet struct ./pnbody_pyo3 <steps> in rnbody_pyo3 folder.
  • Rust cargo run <steps> in rnbody 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.

images/10k_100k.png

images/1m_10m.png