在谈论BigDecimal时,Java编译器是否将乘法运算删除1?BigDecimal:Java编译器能否优化乘以1?
我站在类似于此:
BigDecimal bd = getBigDecimal();//get arbitrary bigDecimal, could be anyone.
bd = bd.multiply(new BigDecimal(getOneTwoOrThree());
凡getOneTwoOrThree方法声明为:
/*
* Always returns Integers 1, 2 or 3.
*/
Integer getOneTwoOrThree(){
//some code
}
所以,基本上。
if getOneTwoOrThree()
returns 1,the compiler will perform the multiplication?或者它会不遵守指示?
这是有点存在的怀疑,但我想我在某个层面早 - 优化。
我认为这里有太多的代码来内联或任何东西。如果JIT能够做到这一点,我会非常惊讶。然而,在这个实现中,乘以1本质上比乘以具有许多数字的数字(更多数字,更多循环的迭代)便宜。 – erickson 2009-04-24 20:21:57