3 ms·
I don't think this is true. I tried the sample out, and gcc, clang, and intel's compiler all generate a cmov for the code instead of a branch with -O2. I don't
by tbirdz 10y ago
I don't think this is true. I tried the sample out, and gcc, clang, and intel's compiler all generate a cmov for the code instead of a branch with -O2. I don't think all these compilers would have used a cmov instead of a branch if the cmov was more expensive than a branch in this case.
https://godbolt.org/g/nyFLwp https://godbolt.org/g/nyFLwp
- amadvance 10y agoIf you use __builtin_expect() the Intel compiler uses a branch. I mean this way: if (__builtin_expect(index >= cap,0)) { ...
- jnordwick 10y agoIt might have something to do with the branch not being part of a loop so the best it can do is assume the branch is random (think something like modding a hash code when it would indeed be random). Ad was pointed out in the thread, recent cpus have reduced the latency of cmov to a cycle. So your result could also depend on your architecture.