rustc and Clang both hand their code to LLVM for optimization and machine code. If the backend is the same, two programs that do the same work should take the same time. I wrote four small programs in both languages, built C++ with Clang 23 (the LLVM major that rustc 1.100 uses) and with GCC 16, and timed them on one machine.
Short version: the language was never the reason for a large gap. Sorting differed by almost 4× because of the algorithm each standard library ships. Word counting differed by 2× because of how std::unordered_map stores entries. With the same algorithm and the same data structure, Rust and C++ ended up within a few percent of each other, and C++ won the streaming test because std::string keeps short words inside the object.
Results
All times are the median of 10 runs (100M-key integer tests: 3 runs), lower is better.
Sort 100M random u64 (file read before the timer starts)
| Rust | Clang | GCC | |
|---|---|---|---|
standard library (sort_unstable / std::sort) | 2.70 s | 10.01 s | 10.23 s |
C++ with pdqsort | 4.02 s | 3.79 s |
Word count, 499 MB file loaded into memory first (61.2M words, 871,375 unique)
| Rust | Clang | GCC | |
|---|---|---|---|
defaults (HashMap with SipHash / std::unordered_map) | 4.70 s | 7.11 s | 7.15 s |
| + reserve | 4.66 s | 6.95 s | 6.90 s |
| same FNV-1a hash on both sides | 3.37 s | 6.93 s | 7.17 s |
C++ absl::flat_hash_map, same hash | 3.57 s | 3.90 s | |
C++ boost::unordered_flat_map, same hash | 3.61 s | 4.00 s |
Word count, file read in 1 MiB chunks (timed end to end)
| Rust | Clang | GCC | |
|---|---|---|---|
| defaults | 4.92 s | 6.11 s | 6.63 s |
| same FNV-1a hash | 3.45 s | 6.12 s | 6.49 s |
C++ absl::flat_hash_map | 2.54 s | 2.82 s | |
C++ boost::unordered_flat_map | 2.81 s | 2.89 s |
Integer keys, insert (same Fx-style hash on every row except “defaults”)
| Rust | Clang | GCC | |
|---|---|---|---|
| 10M, defaults | 879 ms | 3.33 s | 3.33 s |
10M, HashMap / unordered_map | 667 ms | 3.60 s | 3.60 s |
10M, absl::flat_hash_map | 645 ms | 718 ms | |
10M, boost::unordered_flat_map | 1.28 s | 587 ms | |
100M, HashMap / unordered_map | 9.21 s | 49.31 s | 48.34 s |
100M, absl::flat_hash_map | 9.26 s | 11.17 s | |
100M, boost::unordered_flat_map | 28.76 s | 8.05 s |
Parallel word count (64 chunks counted in parallel, then merged on one thread)
| threads | Rust (Rayon) | Clang (OpenMP) | GCC (OpenMP) |
|---|---|---|---|
| 1 | 2.37 s | 2.63 s | 2.72 s |
| 2 | 1.21 s | 1.36 s | 1.77 s |
| 4 | 639 ms | 706 ms | 1.26 s |
| 8 | 385 ms | 424 ms | 767 ms |
| merge (8 threads) | 1.06 s | 857 ms | 953 ms |
The setup
- Machine: DigitalOcean CPU-Optimized droplet, 16 dedicated vCPUs, Intel Xeon Platinum 8358, Ubuntu 26.04.1.
- Toolchains: rustc 1.100.0 (LLVM 23.1.5), Clang 23.1.6, GCC 16.0.1 (snapshot), Abseil 20260817.0, Boost 1.92.0.
- Flags: Rust
--releasewithlto = "fat",codegen-units = 1,target-cpu=native. C++-O3 -DNDEBUG -march=native -flto -std=c++23. The first runs had Abseil’s assertions on;-DNDEBUGfixed that before any number here was recorded. - Rules: every program reads its input from a file, prints JSON with a checksum, and the runner stops if Rust, Clang and GCC builds disagree. Single-threaded tests are pinned to one core with
taskset. One warm-up run, then 10 measured runs. - No async anywhere: the parallel test uses Rayon on the Rust side and OpenMP on the C++ side, both on plain OS threads.
Sorting: the algorithm, not the language
perf stat on the sort, Clang build against Rust:
| counter | Rust sort_unstable | C++ std::sort |
|---|---|---|
| cycles | 11.37 billion | 34.70 billion |
| instructions | 26.69 billion | 22.25 billion |
| instructions per cycle | 2.35 | 0.64 |
| branch misses | 45.7 million | 1,144 million |
C++ ran fewer instructions and still took three times the cycles. Random numbers make every “is this smaller than the pivot?” branch a coin flip, and libstdc++‘s introsort pays a pipeline flush on each wrong guess. Rust’s sort_unstable has been ipnsort since Rust 1.81, a descendant of pdqsort that partitions without data-dependent branches.
Giving C++ pdqsort (Orson Peters’ single header) brought it to 3.8–4.0 s. The remaining gap to 2.70 s is the newer partitioning in Rust’s implementation.
Hash maps: one allocation per entry
Rust’s HashMap hashes with SipHash by default, which protects against crafted keys and costs time. Switching both sides to the same FNV-1a function took Rust from 4.70 s to 3.37 s. C++ stayed at about 7 s, so the hash function was not what slowed it down.
std::unordered_map allocates a separate node for every entry and chains them from a bucket array. The standard requires references to elements to stay valid across rehashing, which pushes implementations to this layout. A lookup walks bucket, then node, then key, and each step can land on a different cache line. With the same hash function:
| counter | Rust HashMap | C++ unordered_map | C++ absl::flat_hash_map |
|---|---|---|---|
| cycles | 11.87 billion | 27.76 billion | 16.76 billion |
| cache misses | 38.5 million | 72.6 million | 44.1 million |
Rust’s HashMap is a port of Abseil’s SwissTable: entries sit side by side in one array, and 16 one-byte control tags are compared with a single SIMD instruction to find candidates. With absl::flat_hash_map or boost::unordered_flat_map, C++ landed at 3.57–3.61 s against Rust’s 3.37 s.
Streaming: where C++ wins
Reading the file in 1 MiB chunks means a word cannot point into one big buffer anymore; each new key has to be copied into the map. Allocation counts from heaptrack:
| build | allocation calls | peak heap |
|---|---|---|
Rust, HashMap<Vec<u8>, u64> with FNV-1a | 871,400 | 44.75 MB |
C++ unordered_map<std::string, …> | 871,400 | 63.59 MB |
C++ absl::flat_hash_map<std::string, …> | 9 | 46.21 MB |
The words in this file are 3 to 12 letters long. libstdc++‘s std::string stores up to 15 characters inside the 32-byte object itself (small string optimization), so a flat C++ map never touches the allocator for a key. Vec<u8> always puts its bytes on the heap. That is the 2.54 s vs 3.45 s result. A Rust crate with an inline small string type would close it; the standard library has none.
Dropping the page cache before each run moved the streaming times by 2–4% (Rust 3.45 → 3.60 s, Clang unordered_map 6.12 → 6.24 s), so the disk was not the bottleneck.
Integer keys
The pattern repeats: unordered_map is 3.5 to 5 times slower on inserts, and the flat designs are close to Rust. Two results stand out:
- Misses: Rust looked up 10M absent keys in 121 ms,
absl::flat_hash_mapneeded 357 ms,boost::unordered_flat_map109 ms. - The same Boost code, two compilers: Boost inserts took 2 to 3.5 times longer with Clang than with GCC (10M: 1.28 s vs 587 ms; 100M: 28.76 s vs 8.05 s).
perf statcounts the same number of instructions and 42% more cycles for the Clang build. I did not isolate the cause.
Parallel: same model, different runtimes
In Rust the change from serial to parallel was iter() to into_par_iter(); in C++ one #pragma omp parallel for. Rayon and Clang’s OpenMP runtime (libomp) scaled almost the same, 385 ms vs 424 ms on 8 threads. GCC’s libgomp was the slowest, 767 ms. The single-threaded merge of the 64 partial maps is not parallel and took longer than the counting at 8 threads on every build.
What this does not cover
One x86 server, one input file per test, these compiler versions and flags. Other CPUs, ARM, other allocators (jemalloc, mimalloc), or -O2 builds can move these numbers. The results say which standard library and data structure choices cost time here, not that one language is faster in general.
Run it yourself
The code, raw JSON results, perf output, flame graphs and heaptrack files are in programming-language-wars/rust-vs-cpp. On a fresh Ubuntu 26.04 machine, as root, with that folder at /root/bench:
./setup.sh # Rust stable, Clang with rustc's LLVM major, newest GCC, Abseil
./setup_boost.sh # Boost unordered_flat_map
python3 data/gen.py /root/data
./build.sh
UNIQUE=871375 python3 bench.py
python3 bench_boost.py
./prof.sh # perf stat, flame graphs, heaptrack
bench.py stops with CHECKSUM MISMATCH if any build prints a different answer, and writes results/summary.json.
Sources
- Rust 1.81.0 release notes: new sort implementations
- Rust
HashMapdocumentation (SipHash, SwissTable) - pdqsort by Orson Peters
- Abseil: SwissTables design notes
- Boost.Unordered:
boost::unordered_flat_map - heaptrack
- Rayon
- Elon Musk on X: “Rust is pretty great…” (February 19, 2026)
- Elon Musk on X: “Real men manage their own memory” (November 10, 2026)