【发布时间】:2018-04-16 05:11:00
【问题描述】:
是否存在效率差异:-
public boolean canDivisionBeDone(int iA, int iB){
try{
float a = iA/iB;
}catch(Exception e){
return false;
}
return true;
}
和
public boolean canDivisionBeDone(int iA, int iB){
if(iB == 0){
return false;
}else{
float a = iA/iB;
}
return true;
}
如果是,为什么?
【问题讨论】:
-
try-catch降低可读性。return iB==0?false:true; -
@HadiJ 我问的是性能效率。在 CPU 消耗、耗时、内存等方面。
-
该示例不太适合比较性能。无论如何,取决于使用它的需要,你不能让它落后于它的性能。
-
@HadiJ 不必要的三元运算符也会降低可读性。 :-)
return iB != 0。虽然我怀疑这只是一个 sn-p,但 OP 在计算后实际上是在用a做一些事情。
标签: java performance try-catch