A collection of benchmarks to compare the performance of algorithms implemented in Rust versus C.
This repository contains multiple implementations of common algorithms in both Rust and C. The goal is to benchmark their performance, compare efficiency between the two languages, and analyze results using profiling tools such as flamegraphs.
Each directory in the repository targets a specific algorithm or operation:
- array_sum/ – Benchmark for summing elements of an array.
- bfs/ – Implementations and benchmarks for Breadth-First Search.
- binary_search/ – Benchmark for binary search algorithms.
- dfs/ – Implementations for Depth-First Search.
- fuse/ – Benchmark for fused operations (if applicable).
- quicksort/ – Comparison of quicksort implementations.
- runtime_check/ – Benchmarks related to runtime safety and checks.
- selection_sort/ – Benchmark for selection sort algorithms.
Each folder typically contains:
- Source code in both Rust and C.
- Makefiles (or Cargo.toml for Rust) for building the benchmarks.
- Performance logs and flamegraph outputs (e.g.,
.svgfiles) for profiling analysis.
Before running the benchmarks, ensure you have the following installed:
- Rust: Install from rustup.rs if not already set up.
- C Compiler: GCC.
- Make: To build the C projects using provided Makefiles.
- perf: Linux performance analysis tool.
- Flamegraph: A tool to generate flamegraphs from perf data. Check out FlameGraph on GitHub for installation instructions.
- Clone the Repository:
git clone https://github.com/GabeBai/Rust_C_Benchmark.git
cd Rust_C_Benchmark- Build a Benchmark:
Navigate to the directory of the desired benchmark (e.g., bfs):
cd bfs- For Rust code:
cargo build --release- For C code:
make- Run and Profile the Benchmark:
Run the benchmark executable to generate performance data. For example:
./bfsTo profile using perf and generate a flamegraph:
//C
perf record -F 99 -g --call-graph=dwarf -o out.perf ./your_program
//Rust
perf record -F 99 -g --call-graph=dwarf -o out.perf ./target/release/your_program
//flamegraph
perf script -f -i out.perf | ../FlameGraph/stackcollapse-perf.pl > out.folded
../FlameGraph/flamegraph.pl out.folded > flamegraph.svg
xdg-open flamegraph.svg
The repository allows you to compare the performance between Rust and C implementations by reviewing:
- Execution Times: How fast each algorithm runs.

- Flamegraphs: Visual representations of the call stack to identify bottlenecks.
Example: BFS

- Rust compile time check vs runtime check

- File System Performance
Details

These insights can help you understand the trade-offs between Rust’s safety features and C’s low-level optimizations.
Contributions are welcome! If you have suggestions, improvements, or additional benchmarks to add, please feel free to submit a pull request or open an issue.
This project is licensed under the MIT License.