[英]Java Long Perfomance on 64-bit JVM
我感興趣的是,許多Long變量在x86-64服務器上部署的4位JVM上使用時的性能如何? 如果在同一服務器上使用相同的Integer,會有什么大的不同?
我的環境:
$ 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卻慢了兩倍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.