请考虑以下方法:
private static long maskAndNegate(long l) {
int numberOfLeadingZeros = Long.numberOfLeadingZeros(l)
long mask = CustomBitSet.masks[numberOfLeadingZeros];
long result = (~l) & mask;
return result;
}
Run Code Online (Sandbox Code Playgroud)
该方法可以缩写为:
private static long maskAndNegate(long l) {
return (~l) & CustomBitSet.masks[Long.numberOfLeadingZeros(l)];
}
Run Code Online (Sandbox Code Playgroud)
这两个表示在实际运行时间是否相等?换句话说,Java编译器是否优化了额外变量的不必要定义,我已将其置于可读性和调试之中?
我只是想,以下方案之间是否有任何性能明智的好处.
情况1
int x = 2;
boolean y = false;
if (x == 2) {
y = true;
}
Run Code Online (Sandbox Code Playgroud)
案例2
int x = 2;
boolean y = (x == 2);
Run Code Online (Sandbox Code Playgroud)
我的想法是,案例1更具可读性.