Kalman filter in Rust runs 120+ times faster than NumPy, SciKit implementation

448 points ivan_kudryavtsev

Hello, fellow rustaceans,

I implemented the Kalman filter for bounding boxes during the last two days. As an inspiration source, I looked at the Python3 Kalman filter implementation from the DeepSORT algorithm repo that uses NumPy and SciKit under the hood, so it's pretty efficient because all the operations are run inside FFI.

What I have found is that the final performance differs 140 times (Intel(R) Core(TM) i5-7440HQ CPU @ 2.80GHz):

Python code Gist: https://gist.github.com/bwsw/84c2c535b1fce8bd19b9b05ea4503dbf

Rust code (Similari framework repo): https://github.com/insight-platform/Similari/blob/main/src/utils/kalman.rs

Numbers (100K iterations):

Python: 9.6s - average over 4 runs at 100K iterations

Rust: 67ms average - average over 4 runs at 100K iterations

Rust code is fully monomorphized, developed with the power of the Nalgebra crate.

The results are inspirational because object trackers are very resource intensive, and the Python implementations are always the bottleneck when more than a few objects are tracked.

So now, I want to develop a Python library that uses a Rust-based SORT tracker under the hood. It's interesting to see how PyO3/Maturin implementation differs from the Python and Rust implementations.