Re:The Weird Concept of Branchless Programming
by Roxxik on Sunday September 28, 2025 @05:36PM
(#45408287)
I also tried myself, on different array sizes, with more iterations. The branchy version is not strictly worse.
https://gist.github.com/Stefan-JLU/3925c6a73836ce841860b55c8...
Re:The Weird Concept of Branchless Programming
by Someone on Monday September 29, 2025 @04:17PM
(#45418236)
> I had to drop down to O0 to see branchless be faster in any case
Did you check whether your branchy code actually still was branchy after the compiler processed it at higher optimization levels?
At a 5ms baseline with millisecond precision, the smallest improvement you can measure is 20%. And you cannot distinguish a 20% speedup with a 20% slowdown that happened to get luck with clock ticks.
For what it is worth, I ran the provided test code on my machine with a 100x increase in iterations and got the following:
Which is not exactly encouraging (gcc 13.3.0, -ffast-math -march=native. I did not use the -fomit-this-entire-function flag, which my compiler does not understand).I had to drop down to O0 to see branchless be faster in any case: