我想知道为什么这段代码返回不同的时间:
我的输出是:
Time1: 511 Time2: 228 for N: 100000000
Time1: 509 Time2: 229 for N: 100000001
Time1: 503 Time2: 229 for N: 100000002
我有:
java version "1.6.0_24"
OpenJDK Runtime Environment (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
OpenJDK Server VM (build 20.0-b12, mixed mode)
Intel® Pentium(R) CPU B960 @ 2.20GHz × 2
Linux Ubuntu 12.04
我的代码如下所示:
public class test {
public static void main(String[] arg)
{
for(long N=100000000;N<2000000000;++N){
long time2 = System.currentTimeMillis();
double d = 1.0;
double z = 1.0/3.0;
for(long i = 0; i < N; i++)
{
d = d*z;
}
long result2 = System.currentTimeMillis() - time2;
long time1 = System.currentTimeMillis();
double x = 1.0;
double f = 1.0/3.0;
for(long i = 0; i < N; i++)
{
x = x*f;
}
long result1 = System.currentTimeMillis() - time1;
System.out.println("Time1: " + result1 + " Time2: " + result2 + " bigger: " + (result1 > result2) + " for N: " + N);
}
}
}