的时间我有这样的代码:爪哇 - 执行
public static void main(String[] args) {
long f = System.nanoTime();
int a = 10 + 10;
long s =System.nanoTime();
System.out.println(s - f);
long g = System.nanoTime();
int b = 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10;
long h =System.nanoTime();
System.out.println(h - g);
}
有了这个输出/秒:
测试1:
427
300
测试2:
533
300
测试3:
431
398
根据我的测试场景,为什么行int b = 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10 + 10;
执行速度比int a = 10 + 10;
更快?
在主方法中的_one_运行结束这是太不确定。至少在一个循环内运行几百万次。 – NilsH
Micheal 请在下面检查URL。 Java中Integer类型的到期池行为会导致后者运行得更快,因为整数b执行的时间10已经在池中。希望这可以帮助。 http://stackoverflow.com/questions/13098143/java-integer-constant-pool –
我每次运行这段代码都会得到不同的结果,从0到600,有时其中一个是0。这些操作太快,您无法一次测试。 – Djon