A C++ Quicksort Faster Than std:sort tiki.li · 13 points · 3 comments · HN threa...
HN Best 200 @best
A C++ Quicksort Faster Than std:sort tiki.li · 13 points · 3 comments · HN thread
TL;DR The author presents blqsort, an MIT-licensed C++ quicksort (single header, with a C version and multithreaded variants) that claims to be faster than std::sort on random data. Benchmarks on 50 million doubles show about 0.68s on an Apple M1 versus 1.33s for std::sort, and the article walks through the techniques behind it: branchless partitioning, sorting networks for small partitions and median-based pivot selection.
- Benchmarks on 50 million random doubles: blqsort 0.68s, ipnsort (ClickHouse port) 1.00s, std::sort 1.33s on an Apple M1 with Clang -O3; on an Intel i5-9300H with GCC, 1.32s, 2.11s and 4.02s.
- Sorting 50 million structs keyed by an int id: blqsort 0.75s versus std::sort 3.45s on the M1; the single-threaded version was used for fairness, and the multithreaded variant is said to be about 3x faster again on the M1.
- Key techniques: branchless partitioning (a comparison result drives a conditional increment instead of a branch), sorting networks for partitions up to 16 elements, and pivots chosen as medians of medians with some randomness.
- Fallbacks: already sorted and reverse-sorted input is handled efficiently, many duplicate keys are grouped, and heap sort is used when partitioning stays very uneven.
- The article also explains a small branchless microbenchmark (0.036s versus 0.345s for the branchy version) and notes that on ARM Macs Clang turns a compact if into csel, but that GCC does not do this on x86.
- The header blqs.h is a drop-in replacement for std::sort with a user-supplied comparator; for non-trivially-copyable types such as strings it switches to a BlockQuicksort-style index-based variant that needs C++17.
Discussion The thread is small, with three top-level comments, and is mostly a side discussion about what makes quicksort fast rather than a debate about blqsort itself. One commenter argues that quicksort's advantage is its near-linear space use and low I/O, and doubts that the 50-million-item benchmark is representative for problems where I/O dominates. Another replies that the O(log n) complexity is the same as ordinary quicksort, and that the real gain here is branchless partitioning on random data. A third points out that radix sort is even faster where it applies, since it is essentially a few sequential scans with good locality. The mood is informal and technical, with no one disputing the benchmark numbers.
- The speedup comes mainly from branchless partitioning (some): One commenter says the main advantage is that branchless partitioning avoids mispredicted branches on random data, which is where ordinary quicksort loses time.
- Quicksort is fast because it minimizes I/O and memory use (some): One commenter argues that quicksort's space needs stay close to n and it moves data little, so benchmarks on 50 million items may mostly reflect I/O behavior, which dominates for many realistic sizes.
- Radix sort is still faster where it applies (a few): One commenter notes that radix sort makes only a few sequential passes over the data with good locality, so it can beat any comparison sort when keys allow it.
- Asymptotic complexity is the same as ordinary quicksort (a few): One commenter points out that blqsort is still O(n log n) on average like standard quicksort, so the gain is a constant-factor one.