问题描述
我目前正在撰写关于 GraalVM Native Image 与在 JVM 上运行的 Jar 相比的优缺点的学士论文。
在其中一个测试中,我正在调用某个方法,该方法分配并填充大小为 10^6 的数组。之后,该函数循环数组并执行算术运算(它是 ackley 函数的变体)。此方法的通常运行时间在 3 到 4 秒之间,但有时该方法会在 50 毫秒后完成(当作为 Native Image 或作为在 JVM 上运行的 jar 文件运行时)。
由于数组是使用 Math.Random() 函数填充的,我不认为这是由于缓存造成的,并且 Native Image 排除了 JIT 编译作为这些异常值的来源。
端点看起来像这样,其中 dtno 是包含“范围”变量的数据传输对象:
ackley 函数如下所示:
public static long ackley(int range){
long start = System.nanoTime();
if(range!=0){
double[] a = new double[range];
int counter = 0;
for(int i=-range/2;i<range/2;i++){
a[counter++] = Math.random()*range*i;
}
double sum1 = 0.0;
double sum2 = 0.0;
for (int i = 0 ; i < a.length ; i ++) {
sum1 += a[i]*a[i];
sum2 += (Math.cos(2*Math.PI*a[i]));
}
double result = -20.0*Math.exp(-0.2*Math.sqrt(sum1 / ((double )a.length))) + 20
- Math.exp(sum2 /((double)a.length)) + Math.exp(1.0);
}
long end = System.nanoTime();
return end - start;
}
如前所述,测试中的范围变量是 10^6。我还怀疑的是,由于实际上从未使用 result 和 sum 来计算返回值,因此程序决定跳过 for 循环和“end”的声明之间的所有内容。
在 JMeter 测试的图表中,您可以看到这些快速执行时间都在加速和测试运行的最后阶段。
Performance Test Summary Report
如果有人能给我一个提示或一个好的来源,我可以在那里找到关于正在发生的事情的提示,我将非常感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)