Your code is fast - if you’re lucky
Recently, while I was working on an optimized Quicksort implementation, I came across a rather interesting quirk. Modern compilers (especially Clang) optimize loops using fast, branch-free instructions - provided you use the right programming style.
sort.h - a quicksort with sorting networks
A few cosmetic changes
It is already micro-optimized using sorting networks and loop unrolling. Only a few cosmetic changes remain.
We rewrite this beginner‑friendly style, which explicitly shows how the pointers are moved:
But what actually happened?
This “small cosmetic” change causes Clang to replace branches with csel.
With branches
GCC does not exhibit this “quirk” (different code generation for logically equivalent source). It consistently generates the slower branch-based version.
Links
blqsort - Fast Quicksort with C and C++ Interface
blqsort - Fast Quicksort with C and C++ Interface
When ‘if’ slows you down, avoid it
When ‘if’ slows you down, avoid it
Interactive sorting demo
christof.kaser@gmail.com