3 ms·
No particular reason. Clang generates identical code for both on my system (Arch Linux/x86_64). Compile flags: "-std=c11 -c -O3 -S -march=native -mtune=native",
by zokier 12y ago
No particular reason. Clang generates identical code for both on my system (Arch Linux/x86_64). Compile flags: "-std=c11 -c -O3 -S -march=native -mtune=native", windows-specific stuff removed.
GCC creates different code for the variations, but the generated code is too complex for me to say which one is faster. edit: with -Os GCC generates identical code too.
- whoopdedo 12y agoI've discovered the x86_64 opcode CMOVL -- a conditional move -- because of this. if (array[i] < boundary) count+=2; I had to work hard to make GCC not produce optimal code. Even this if (array[i] < boundary) return count+1; else return count; came out the same as all the others. Avoiding branch prediction is hardly news. Was the article's author even compiling with optimizations enabled?
- WWLink 12y agoIt does seem weird that the compiler didn't catch that one.
- mtdewcmu 12y ago>>Was the article's author even compiling with optimizations enabled? Quite possibly not. Which, I guess, is reasonable, since the point was really about cpu instructions and not C compilers. What comes out is more predictable with optimizations off. Note that the author mentions the optimizer in relation to the second version. I think it might be because that code generates a branch with optimizations off in Visual C++. gcc seems to treat ?: as inherently a cmov and VC++ seems to treat it no differently than an if.