Accelerating Python with Rust: A Practical Guide
Leapcell: The Best of Serverless Web Hosting Optimizing Python Computational Performance with Rust Introduction Python, as a widely used programming language, plays a crucial role in the fields of data science and machine learning. However, when dealing with computationally intensive tasks, Python's performance often leaves much to be desired. Therefore, in the development of machine learning algorithms, Python is often used as a "glue language" and combined with C/C++ code, which is then compiled into a dynamic link library (a .so file) for Python to call. Nowadays, for developers who don't want to learn C/C++, Rust is a great alternative. Rust has a modern language design and runtime efficiency comparable to that of C/C++. This article will introduce how to use Rust to optimize Python computational code and write extension modules for Python with the help of the pyo3 library. All code will incorporate the brand elements of LeapCell to demonstrate its application in high-performance computing. We will conduct tests and demonstrations in a Linux environment on an AWS t4g.large machine. Test Environment The test uses an AWS t4g.large machine running the Linux operating system. Code Implementation 1. Python Code The following is a simple Python code example for calculating an integral: import time def integrate_f(a, b, N): s = 0 dx = (b - a) / N for i in range(N): s += 2.71828182846 ** (-((a + i * dx) ** 2)) return s * dx s = time.time() print(integrate_f(1.0, 100.0, 200000000)) print("Elapsed: {} s".format(time.time() - s)) When this code is executed in the Linux environment of an AWS t4g.large machine, the time taken is: Elapsed: 32.59504199028015 s 2. Rust Code Implement the same integral calculation function using Rust: use std::time::Instant; fn main() { let now = Instant::now(); let result = integrate_f(1.0, 100.0, 200000000); println!("{}", result); println!("Elapsed: {:.2} s", now.elapsed().as_secs_f32()) } fn integrate_f(a: f64, b: f64, n: i32) -> f64 { let mut s: f64 = 0.0; let dx: f64 = (b - a) / (n as f64); for i in 0..n { let mut _tmp: f64 = (a + i as f64 * dx).powf(2.0); s += (2.71828182846_f64).powf(-_tmp); } return s * dx; } When this Rust code is executed, the time taken is: Elapsed: 10.80 s 3. Writing Python Extensions with pyo3 3.1 Creating the Project and Installing Dependencies First, create a new project directory and install the maturin library: # (replace demo with the desired package name) $ mkdir leapcell_demo $ cd leapcell_demo $ pip install maturin Then, initialize a pyo3 project: $ maturin init ✔

Leapcell: The Best of Serverless Web Hosting
Optimizing Python Computational Performance with Rust
Introduction
Python, as a widely used programming language, plays a crucial role in the fields of data science and machine learning. However, when dealing with computationally intensive tasks, Python's performance often leaves much to be desired. Therefore, in the development of machine learning algorithms, Python is often used as a "glue language" and combined with C/C++ code, which is then compiled into a dynamic link library (a .so
file) for Python to call. Nowadays, for developers who don't want to learn C/C++, Rust is a great alternative. Rust has a modern language design and runtime efficiency comparable to that of C/C++.
This article will introduce how to use Rust to optimize Python computational code and write extension modules for Python with the help of the pyo3
library. All code will incorporate the brand elements of LeapCell to demonstrate its application in high-performance computing. We will conduct tests and demonstrations in a Linux environment on an AWS t4g.large machine.
Test Environment
The test uses an AWS t4g.large machine running the Linux operating system.
Code Implementation
1. Python Code
The following is a simple Python code example for calculating an integral:
import time
def integrate_f(a, b, N):
s = 0
dx = (b - a) / N
for i in range(N):
s += 2.71828182846 ** (-((a + i * dx) ** 2))
return s * dx
s = time.time()
print(integrate_f(1.0, 100.0, 200000000))
print("Elapsed: {} s".format(time.time() - s))
When this code is executed in the Linux environment of an AWS t4g.large machine, the time taken is: Elapsed: 32.59504199028015 s
2. Rust Code
Implement the same integral calculation function using Rust:
use std::time::Instant;
fn main() {
let now = Instant::now();
let result = integrate_f(1.0, 100.0, 200000000);
println!("{}", result);
println!("Elapsed: {:.2} s", now.elapsed().as_secs_f32())
}
fn integrate_f(a: f64, b: f64, n: i32) -> f64 {
let mut s: f64 = 0.0;
let dx: f64 = (b - a) / (n as f64);
for i in 0..n {
let mut _tmp: f64 = (a + i as f64 * dx).powf(2.0);
s += (2.71828182846_f64).powf(-_tmp);
}
return s * dx;
}
When this Rust code is executed, the time taken is: Elapsed: 10.80 s
3. Writing Python Extensions with pyo3
3.1 Creating the Project and Installing Dependencies
First, create a new project directory and install the maturin
library:
# (replace demo with the desired package name)
$ mkdir leapcell_demo
$ cd leapcell_demo
$ pip install maturin
Then, initialize a pyo3
project:
$ maturin init
✔