【发布时间】:2010-11-09 22:45:54
【问题描述】:
谈论java性能..什么更好? if..else 或多个简单的 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;
}
或
if( condition ) {
some_code;
return value;
}
if( condition ) {
some_code;
return value;
}
if( condition ) {
some_code;
return value;
}
some_code;
return value;
对你的想法感兴趣
谢谢!
【问题讨论】:
-
如果有性能差异,大概可以忽略不计。
-
我不是专家,但如果有任何区别,我会感到惊讶——但所有性能问题的答案都是“自己试试看”。 (另一个答案是“这可能不是你的瓶颈。”)这对我来说似乎是一个 style 问题。
-
不管“some_code”,只有java如何执行“if”语句
-
您还可以选择在
if...else if..示例的末尾添加一个return。 -
假设返回值可能不同
标签: java if-statement performance