Member-only story
Create a Python Package with Super- Fast Rust Code in 3 Steps
Extend you Python code with a package containing Rust code for a >150x performance increase!
Python is a pretty easy language to pick up and it’s super quick to write some code in, compared to some other languages. All this ease-of-use comes with a downside: speed is sacrificed. Sometimes Python is just too slow!
To solve this problem we’ll re-write a bit of our Python-code in Rust and import this code, as a Python package, into our original project. We end up with a super-fast Python package that we can import and use like any other package. As a bonus we’ll multi-process our Rusty Python package and end up with a function that is roughly 150x faster. Let’s code!
Overview
A quick summary of what we’re going to do in this article. We’ll tackle the problem in 6 steps (of which step 2, 3 and 4 are devoted to actually writing the package):
- Examining our slow function; why is it slow?
- Preparing our project
- We re-write this function in Rust
- Compile the Rust code and put it in a Python package
- Import the Python package into our project