【发布时间】:2015-10-09 14:20:56
【问题描述】:
我有以下几点:
double timeInMinutes = (double) timeInMilliseconds / (1000 * 60);
操作(1000 * 60) 是在编译时还是在运行时完成的?换句话说,上面的代码sn-p和以下代码在运行时是否存在性能差异:
double timeInMinutes = (double) timeInMilliseconds / 60000;
编辑:我的问题与Will the Java compiler precalculate sums of literals? 不同,因为我在算术运算中混合使用变量和文字。这是一个很小的差异,但正如 @TagirValeev 在 cmets (Are arithmetic operations on literals calculated at compile time or run time?) 中指出的那样,在某些情况下,即使可以预编译某些文字。
【问题讨论】:
-
查找常量表达式。
-
这是编译器可以实现的最简单的优化之一。即使是旧的“未优化”VB3-VB6 也曾经这样做过(不确定早期版本,他们可能也这样做过)。
-
您误解了 TagirVallev 的观点:
(double) time / 1000 / 60不是对文字的操作,并且出于浮点原因,甚至不能期望给出与(double) time / (1000 * 60)相同的结果。
标签: java