なぜこのコードが異なる時間を返すのか疑問に思います:
私の出力は次のとおりです。
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);
}
}
}