什么是更好的? if..else或多个简单if [英] What is better? if..else or multiple simple if
问题描述
if(condition){
some_code;
返回值;
}
else if(condition){
some_code;
返回值;
}
else if(condition){
some_code;
返回值;
}
else {
some_code;
返回值;
}
或
if(condition){
some_code;
返回值;
}
if(condition){
some_code;
返回值;
}
if(condition){
some_code;
返回值;
}
some_code;
返回值;
对您的想法感兴趣
Thnx!
性能方面没有区别。选择最易读的选项,可能取决于代码的作用。
一般不要担心这些微优化。只有在您确定存在需要修复的性能问题后才能进行优化。
我们应该忘记效率低,大约97%的时间说:过早优化是所有邪恶的根源。 — Donald Knuth
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
这篇关于什么是更好的? if..else或多个简单if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!