HN user

chrka

175 karma

christof.kaser@gmail.com

Posts17
Comments11
View on HN
tiki.li 11d ago

Your code is fast if you're lucky

chrka
136pts85
tiki.li 29d ago

Your code is fast – if you're lucky

chrka
2pts0
easylang.online 2mo ago

Show HN: Faster than std:sort and pdqsort

chrka
1pts0
easylang.online 2mo ago

Show HN: Branchless Quicksort – faster than std:sort and pdqsort

chrka
2pts0
easylang.online 2mo ago

Show HN: Avoiding "if" makes Quicksort faster

chrka
3pts4
easylang.online 2mo ago

When 'if' slows you down, avoid it

chrka
6pts0
easylang.online 2mo ago

Show HN: A Fast Quicksort in C with Branch‑Avoidant Coding and Threads

chrka
1pts0
easylang.online 3mo ago

Show HN: Interactive demos of sorting algorithms with runtime comparisons

chrka
1pts0
easylang.online 3mo ago

Show HN: Interactive sorting algorithm visualizations with runtime comparisons

chrka
1pts0
easylang.online 6mo ago

Show HN: Law of Large Numbers or Why It's a Bad Idea to Go to a Casino

chrka
3pts1
easylang.online 7mo ago

Show HN: Programming a Christmas Tree

chrka
2pts0
easylang.online 8mo ago

Show HN: A short story of my programming language Easylang

chrka
2pts0
ras.ac.uk 8mo ago

Analysis indicates that the universe’s expansion is not accelerating

chrka
261pts208
easylang.online 8mo ago

Show HN: Strange Attractors – Visualized with Easylang

chrka
3pts0
easylang.online 8mo ago

Show HN: A beginner-programming language and IDE with helpful tab completion

chrka
3pts0
chriszetter.com 8mo ago

Splitting (Empty) Strings (2017)

chrka
1pts0
easylang.online 9mo ago

Show HN: Tab completion for my small browser-based programming language Easylang

chrka
2pts0

Both versions use the same input data. I also tried different random initial values and got essentially the same result. I didn't test hundreds of inputs, since that would have been mostly a waste of time in this case. The algorithm and the data distribution remain practically the same. What I'm measuring is the machine code that Clang generates for the hot loop.

You're talking about the complexity of the Quicksort algorithm, whereas the article is about code generation.

Both versions sort the same data using the same algorithm. Just a tiny change in the source code caused Clang to generate different machine code.

Using different seed values - (srand(1), srand(2), srand(time(NULL))) essentially leads to the same result. With a good choice of pivot, Quicksort is very close to O(n log n) in practice, so that’s not the key factor here.

The interesting thing is that the generated machine code changes significantly.

Normally, quicksort works best on random data. But with 90% already sorted and 10% random, it actually becomes harder to pick a good pivot. Sometimes the pivot ends up too large, which creates very uneven splits. When that happens, the algorithm switches to heapsort to avoid worst-case behavior, but heapsort is slower. Now, instead of immediately switching, it tries to partition again. Only if it’s still bad does it fall back to heapsort. That’s why performance improves.