我正在进行一项小测试,虽然它是一个微型基准测试,但它确实模仿了我们在生产中的实际效果。
我正在创建一个2D数组,5列和10,000,000行,填充0-19(含)之间的随机整数。然后我想总结第3列中的所有数字,只要第2列中的值是偶数。我做了100次热身,然后又做了100次,花了多长时间。
在我的机器上,绝大部分时间大约需要9秒,但偶尔需要不到6秒。
它看起来不像垃圾收集或JIT编译。
有没有人知道为什么偶尔会这么快得多?
我使用以下参数在Linux上使用JDK7u11运行代码:-server -XX:+ PrintCompilation -Xms500m -Xmx500m -verbose:gc -XX:+ PrintGCTimeStamps -XX:+ PrintGCDetails但是,使用各种不同的JDK(从6开始) 8)并删除所有这些参数似乎并没有显着影响时间。
以下是代码:
import java.util.ArrayList;
import java.util.Random;
public class JavaPerformanceTest {
public static void main(String[] args) {
int numColumns = 5;
int numRows = 10000000;
int[][] data = new int[numColumns][numRows];
Random rand = new Random(1234);
for (int j = 0; j < numColumns; j++) {
for (int i = 0; i < numRows; i++) {
data[j][i] = rand.nextInt(20);
}
}
int warmUp = 100;
ArrayList<Integer> sums = new ArrayList<Integer>();
System.out.println("warm up " + warmUp + " times");
long warmUpStart = System.nanoTime();
for (int i = 0; i < warmUp; i++) {
sums.add(sum(numRows, data));
}
long warmUpEnd = System.nanoTime();
System.out.println("warm up complete " + (warmUpEnd - warmUpStart) / 1000000);
int numberOfRuns = 100;
int finalSum = 0;
long startTime = System.nanoTime();
for (int i = 0; i < numberOfRuns; i++) {
finalSum = sum(numRows, data);
}
long endTime = System.nanoTime();
long diff = (endTime - startTime) / 1000000;
System.out.println("Time taken: " + diff + " Sum: " + finalSum);
}
public static int sum(int numRows, int[][] columnBased) {
int sum = 0;
for (int i = 0; i < numRows; i++) {
if ((columnBased[1][i] % 2) == 0) {
sum += columnBased[2][i];
}
}
return sum;
}
}
谢谢,尼克。
答案 0 :(得分:1)
导致性能降低的原因有很多,包括缓存未命中和分支预测失败。我会确保你的代码是最优的,然后重复它以确保结果稳定。
import java.util.ArrayList;
import java.util.Random;
public class JavaPerformanceTest {
public static void main(String[] args) {
int numColumns = 5;
int numRows = 10000000;
byte[][] data = new byte[numColumns][numRows];
Random rand = new Random(1234);
for (int j = 0; j < numColumns; j++) {
for (int i = 0; i < numRows; i++) {
data[j][i] = (byte) rand.nextInt(20);
}
}
int warmUp = 10;
ArrayList<Integer> sums = new ArrayList<Integer>();
System.out.println("warm up " + warmUp + " times");
long warmUpStart = System.nanoTime();
for (int i = 0; i < warmUp; i++) {
sums.add(sum(numRows, data));
}
long warmUpEnd = System.nanoTime();
System.out.println("warm up complete " + (warmUpEnd - warmUpStart) / 1000000);
for (int t = 0; t < 3; t++) {
int numberOfRuns = 100;
int finalSum = 0;
long startTime = System.nanoTime();
for (int i = 0; i < numberOfRuns; i++) {
finalSum = sum(numRows, data);
}
long endTime = System.nanoTime();
long diff = (endTime - startTime) / 1000000;
System.out.println("Time taken: " + diff + " Sum: " + finalSum);
}
}
public static int sum(int numRows, byte[][] columnBased) {
int sum = 0;
byte[] col1 = columnBased[1];
byte[] col2 = columnBased[2];
for (int i = 0; i < numRows; i++)
// use multiplication instead of "if" to avoid branch prediction failures
sum += ((col1[i] + 1) & 1) * col2[i];
return sum;
}
}
打印
warm up 10 times
warm up complete 109
Time taken: 1006 Sum: 47505460
Time taken: 1006 Sum: 47505460
Time taken: 1026 Sum: 47505460
总之:优化代码将比使用命令行参数更能提高性能。