java - Handling for loop special cases -


in java, c# or c++, suppose have common situation need iterate large amount of times , execute function dox, on 1 of iterations should execute function doy.

int index = 123456; for(int = 0; < 1000000; i++) {     if(i == index) doy();     else dox(); } 

in situations see real performance issue, break loop in 2, can painful, if loop's body large. compiled code check condition on every iteration, or can optimized compiler? furthermore, if index not constant @ compile time, can there such optimizations?

this won't cause huge performance issue. due branch predicting. refer famous question.

branch predicting assembly's way of guessing way if-statement evaluate to. if guesses right, take no time. if guesses wrong, backtrack , cause performance issues. branch predictor use it's previous branched route "guess" next branch.

since if-statement evaluates false time. branch predictor predict correctly every time.

so answer question "does compiled code check condition on every iteration?". no not. though it's not optimized compiler, assembly pipeline itself.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -