std::sort on uint64_t: timings against Rust's sort_unstable

Romain Pinsolle contact@romain-pinsolle.fr
Sat Dec 5 14:59:00 GMT 2026


Hi,

Some measurements with GCC 16.2.1 20260810, to put a number on what the
current introsort costs on integers.

std::sort on uint64_t against Rust's slice::sort_unstable (rustc
1.100.1), on the same data: same generator and same seeds on both
sides, and the checksum of the first array of each shape matches.
Nanoseconds per element:

n = 10,000 n = 1,000,000
input std::sort Rust std::sort Rust
random 53.1 17.9 79.0 23.4
sorted 9.5 0.8 13.1 0.8
reversed 6.6 1.1 9.3 1.2
16 distinct values 25.8 4.6 27.6 4.9
sorted, 1% swapped 10.8 20.4 15.1 24.1

On random keys std::sort takes about 3 times as long at 10,000 elements
and 3.4 times as long at 1,000,000. It wins on the last line: Rust
takes 1.9 times as long at 10,000 and 1.6 times at 1,000,000.

Setup: AMD Ryzen 5 4600H, Arch Linux, process pinned to one core with
taskset. g++ -std=c++20 -O3 -march=native and rustc -C opt-level=3
-C target-cpu=native. Each figure is a median: 5 rounds of 2 arrays at
1,000,000, 9 rounds of 200 arrays at 10,000. Every array has its own
seed, because sorting the same small array again and again lets the
branch predictor learn it and flatters every algorithm. Random keys
come from splitmix64. "reversed" is v[i] = n - i, "16 distinct values"
is a random key mod 16, and the last line is 0..n-1 after n/100 + 1
swaps of two random positions.

Two things I did not measure: -O2, and pdqsort itself. So this shows
the gap to another standard library's unstable sort, and says nothing
precise about what a pdqsort port would gain in libstdc++.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://gcc.gnu.org/pipermail/libstdc++/attachments/20261205/95abf719/attachment.htm>


More information about the Libstdc++ mailing list