3 ms·
Yeah, easy to predict branches are really cheap. Very non-deterministic, hard-to-predict ones on the other hand are still surprisingly expensive. In such a case
by lorenzhs 11y ago
Yeah, easy to predict branches are really cheap. Very non-deterministic, hard-to-predict ones on the other hand are still surprisingly expensive. In such a case, conditional moves might make sense, or sometimes you can also use the result of a comparison in arithmetic operations, something like:
idx = 2*idx + (key > table[idx])
(to move a key down an implicitly-stored binary tree)
- a_e_k 11y agoI remember Linus making much the same point: http://yarchive.net/comp/linux/cmov.html http://yarchive.net/comp/linux/cmov.html It generally lines up with what I've observed. Surprisingly, I've found that even arithmetic comparisons like you mentioned ran a bit faster with branches instead of conditional moves. The one case where I have seen comparisons benefit is with SSE code where the comparison results go straight to a register instead of a flag.