This project implements the bubble sort algorithm in Rust and provides Python bindings for it using the ctypes library. It also includes a native Python implementation of bubble sort.
The primary purpose of this project is to:
- Demonstrate how to create Rust functions that can be called from Python (Foreign Function Interface - FFI).
- Compare the performance of the Rust implementation versus the native Python implementation for the bubble sort algorithm.
The core bubble sort algorithm is implemented in Rust, located in the rust_bubble directory.
- Source Code:
rust_bubble/src/lib.rs - Function:
bubble_sort(arr: *mut f64, len: i32) -> i32- This function sorts an array of
f64(double-precision floating-point numbers) in place. - It's exposed as a C-compatible function for use by Python.
- This function sorts an array of
To compile the Rust library:
- Navigate to the Rust directory:
cd rust_bubble - Build the project (for a release version, which is optimized):
cargo build --release
- After a successful build, the compiled library will be located at
rust_bubble/target/release/librust_bubble.dylib(on macOS),rust_bubble/target/release/librust_bubble.so(on Linux), orrust_bubble/target/release/rust_bubble.dll(on Windows). - This library needs to be copied to the
lib/directory in the project root for the Python script to find it. For example, on macOS:Adjust the command based on your operating system and the exact library name. The Python script currently expectscp rust_bubble/target/release/librust_bubble.dylib ../lib/
lib/librust_bubble.dylib. If you are on a different OS, you might need to adjust the library name insrc/bubble/__init__.pyor rename the compiled file tolibrust_bubble.dylibin thelibdirectory.
The Python part of the project is located in src/bubble/__init__.py.
- Loading the Rust Library: The script uses Python's
ctypesmodule to load the compiled Rust shared library (e.g.,lib/librust_bubble.dylib).import ctypes lib = ctypes.CDLL("lib/librust_bubble.dylib")
- Defining Argument and Return Types: It then defines the argument types (
argtypes) and return type (restype) for thebubble_sortfunction to ensure correct data marshalling between Python and Rust.# Assuming 'lib' is the loaded library from ctypes lib.bubble_sort.argtypes = [ctypes.POINTER(ctypes.c_double), ctypes.c_int] lib.bubble_sort.restype = ctypes.c_int
- Native Python Implementation: The script also includes a native Python implementation of bubble sort,
bubble_sort_by_python3(arr: Sequence[float]) -> list[float], for comparison purposes. - Calling the Rust function: To call the Rust function, a Python list of floats is converted into a C-compatible array of doubles, and then passed to the
lib.bubble_sortfunction.# Example: data = [4.0, 2.0, 5.0, 1.0, 3.0] array_type = ctypes.c_double * len(data) c_array = array_type(*data) result = lib.bubble_sort(c_array, len(data)) if result == 0: sorted_data_from_rust = list(c_array) print(f"Sorted array (Rust): {sorted_data_from_rust}") else: print("Error during Rust sorting.")
The src/bubble/__init__.py script contains a benchmark that uses the timeit module to compare the execution speed of the Rust-based bubble sort and the native Python bubble sort.
To run the benchmark:
-
Ensure you have built the Rust library and copied it to the
lib/directory as described in the "Rust Component" section. -
From the project root directory, execute the Python script as a module:
python -m src.bubble
-
Output: The script will print the total execution time for a large number of sorts (currently 100,000 iterations as per the script) for both the Python version and the Rust version. This allows for a direct performance comparison.
Example (the exact times will vary based on your hardware):
執行時間: X.XXXXXX 秒 // Python version 執行時間: Y.YYYYYY 秒 // Rust version(Note: "執行時間" is Chinese for "execution time", as seen in the current script output.)
To build and run this project, you will need:
- Rust:
- Installed from https://www.rust-lang.org/tools/install
- This includes
rustc(the Rust compiler) andcargo(the Rust package manager and build tool).
- Python 3:
- Installed from https://www.python.org/downloads/
- The
ctypesmodule is part of the standard library, so no additional Python packages are strictly required to run the core functionality. - The benchmark uses
timeit, which is also part of the standard library.
.
├── README.md # This file
├── lib/
│ └── librust_bubble.dylib # Compiled Rust library (example for macOS)
├── pyproject.toml # Python project configuration (if used, e.g., for Poetry or Flit)
├── rust_bubble/
│ ├── Cargo.toml # Rust project manifest
│ ├── src/
│ │ └── lib.rs # Rust bubble sort implementation
│ └── target/ # Rust build artifacts
└── src/
└── bubble/
└── __init__.py # Python script with FFI bindings and benchmark
README.md: Provides information about the project.lib/: Contains the compiled Rust shared library. The Python script expects the library to be here.pyproject.toml: Standard Python project metadata file. (Though in this project it seems minimal or for future use).rust_bubble/: Contains the Rust source code and build configuration.rust_bubble/Cargo.toml: Defines the Rust package and its dependencies.rust_bubble/src/lib.rs: The Rust library code.
src/bubble/: Contains the Python source code.src/bubble/__init__.py: The main Python script that loads the Rust library, defines a Python bubble sort, and runs the performance benchmark.