0

x86-64 サーバーにデプロイされた 4 ビット JVM で使用される場合、多くの Long 変数のパフォーマンスはどのようなものでしょうか? 同じサーバーで同じ整数を使用した場合、大きな違いはありますか?

4

1 に答える 1

1

私の環境:

$ java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

これは私が今実行したコードです:

import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;

public class Performance extends SimpleBenchmark {
  static final Random rnd = new Random();
  static long iters = 100_000_000 + rnd.nextInt(10_000_000);

  public long timeLong(int reps) {
    long sum = rnd.nextLong();
    for (int rep = 0; rep < reps; rep++)
      for (long l = 0; l < iters; l++) sum ^= l;
    return sum;
  }
  public int timeInt(int reps) {
    int sum = rnd.nextInt();
    int iters = (int) Performance.iters;
    for (int rep = 0; rep < reps; rep++)
      for (int l = 0; l < iters; l++) sum ^= l;
    return sum;
  }

  public static void main(String... args) {
    Runner.main(Performance.class, args);
  }
}

そして、これらは結果です。

 0% Scenario{vm=java, trial=0, benchmark=Long} 105721736.11 ns; σ=1752926.46 ns @ 10 trials
50% Scenario{vm=java, trial=0, benchmark=Int} 71749350.00 ns; σ=188518.06 ns @ 3 trials

benchmark    ms linear runtime
     Long 105.7 ==============================
      Int  71.7 ====================

に置き換える^=+=、違いはさらに大きくなります。

benchmark    ms linear runtime
     Long 109.1 ==============================
      Int  53.8 ==============

これは、XOR自体も同様に高速ですが、ADDは2倍遅いことを意味する場合があります。

于 2012-12-11T20:02:55.587 に答える