talking about java performance .. what is better? if..else or multiple simple if
if( condition ) {
some_code;
return value;
}
else if( condition ) {
some_code;
return value;
}
else if( condition ) {
some_code;
return value;
}
else {
some_code;
return value;
}
or
if( condition ) {
some_code;
return value;
}
if( condition ) {
some_code;
return value;
}
if( condition ) {
some_code;
return value;
}
some_code;
return value;
Interested in your thoughts
Thnx !
There is no difference, performance-wise. Choose the most readable option, which could be either depending on what the code does.
In general do not worry about these micro-optimizations. Optimization should only come after you've determined there is a performance problem that needs to be fixed.
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." —Donald Knuth
The if...else example you give doesn't need the return
s if this is in a function with no return value. In that case, the if...else will be easier to read.
Furthermore, the if...else should be preferred because it makes explicit that these cases are mutually exclusive.
If there's a performance difference here, then your compiler/interpreter sucks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With