Skip to content
Huseyin Babal
Go back

Rust vs C++ on the same LLVM: four programs, one 16-core server

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)

RustClangGCC
standard library (sort_unstable / std::sort)2.70 s10.01 s10.23 s
C++ with pdqsort4.02 s3.79 s

Word count, 499 MB file loaded into memory first (61.2M words, 871,375 unique)

RustClangGCC
defaults (HashMap with SipHash / std::unordered_map)4.70 s7.11 s7.15 s
+ reserve4.66 s6.95 s6.90 s
same FNV-1a hash on both sides3.37 s6.93 s7.17 s
C++ absl::flat_hash_map, same hash3.57 s3.90 s
C++ boost::unordered_flat_map, same hash3.61 s4.00 s

Word count, file read in 1 MiB chunks (timed end to end)

RustClangGCC
defaults4.92 s6.11 s6.63 s
same FNV-1a hash3.45 s6.12 s6.49 s
C++ absl::flat_hash_map2.54 s2.82 s
C++ boost::unordered_flat_map2.81 s2.89 s

Integer keys, insert (same Fx-style hash on every row except “defaults”)

RustClangGCC
10M, defaults879 ms3.33 s3.33 s
10M, HashMap / unordered_map667 ms3.60 s3.60 s
10M, absl::flat_hash_map645 ms718 ms
10M, boost::unordered_flat_map1.28 s587 ms
100M, HashMap / unordered_map9.21 s49.31 s48.34 s
100M, absl::flat_hash_map9.26 s11.17 s
100M, boost::unordered_flat_map28.76 s8.05 s

Parallel word count (64 chunks counted in parallel, then merged on one thread)

threadsRust (Rayon)Clang (OpenMP)GCC (OpenMP)
12.37 s2.63 s2.72 s
21.21 s1.36 s1.77 s
4639 ms706 ms1.26 s
8385 ms424 ms767 ms
merge (8 threads)1.06 s857 ms953 ms

The setup

Sorting: the algorithm, not the language

perf stat on the sort, Clang build against Rust:

counterRust sort_unstableC++ std::sort
cycles11.37 billion34.70 billion
instructions26.69 billion22.25 billion
instructions per cycle2.350.64
branch misses45.7 million1,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:

counterRust HashMapC++ unordered_mapC++ absl::flat_hash_map
cycles11.87 billion27.76 billion16.76 billion
cache misses38.5 million72.6 million44.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:

buildallocation callspeak heap
Rust, HashMap<Vec<u8>, u64> with FNV-1a871,40044.75 MB
C++ unordered_map<std::string, …>871,40063.59 MB
C++ absl::flat_hash_map<std::string, …>946.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:

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


Share this post:

Previous Post
Redis 8.10 vs Valkey 9.1 under load: the same Go app, the same 4-core machine