It's only factor 2 with an increasing array though. At which point you can just take the last element, that's way faster.
So really you end up having to make assumptions about the input to get the performance boost.
So really you end up having to make assumptions about the input to get the performance boost.
The CPU's branch predictor won't be able to perform that kind of algorithmic analysis, but patterns like the above also work reasonably well for simpler heuristics like 'predict the same outcome as the last time the branch was taken'.