【发布时间】:2016-12-25 07:25:15
【问题描述】:
我正在尝试用一个简单的函数来衡量 CPU 计算能力,而不是在我的项目中添加包含数千行和一些 MB 的庞大框架。
我开发了这个示例代码。这是一项完成 100 次艰巨任务的工具。这个巨大的任务(benchmark() 函数)包含一段时间,它将在 100 毫秒内执行数学复杂计算并增加一个计数器。每 100 毫秒,它会在日志中打印 benchmark() 函数完成复杂数学计算的次数。
好的,所以,我在屏幕上打印了 100 次计算。
我第一次执行基准测试时,我得到了正确的结果,对于 100 次基准测试迭代中的每一次,或多或少相同的日志结果:
08-18 13:09:52.806 26543-27748/com.mytestapp D/XXXX: Iteration: 0 Result: 118200
08-18 13:09:52.906 26543-27748/com.mytestapp D/XXXX: Iteration: 1 Result: 171580
08-18 13:09:53.006 26543-27748/com.mytestapp D/XXXX: Iteration: 2 Result: 170654
08-18 13:09:53.106 26543-27748/com.mytestapp D/XXXX: Iteration: 3 Result: 168676
08-18 13:09:53.206 26543-27748/com.mytestapp D/XXXX: Iteration: 4 Result: 168372
08-18 13:09:53.306 26543-27748/com.mytestapp D/XXXX: Iteration: 5 Result: 165558
08-18 13:09:53.406 26543-27748/com.mytestapp D/XXXX: Iteration: 6 Result: 171368
08-18 13:09:53.506 26543-27748/com.mytestapp D/XXXX: Iteration: 7 Result: 171680
08-18 13:09:53.606 26543-27748/com.mytestapp D/XXXX: Iteration: 8 Result: 171516
08-18 13:09:53.706 26543-27748/com.mytestapp D/XXXX: Iteration: 9 Result: 171598
但在一些处决之后,数量开始减少,我不明白为什么:
08-18 13:10:20.850 26543-28161/com.mytestapp D/XXXX: Iteration: 1 Result: 94320
08-18 13:10:20.951 26543-28161/com.mytestapp D/XXXX: Iteration: 2 Result: 90364
08-18 13:10:21.051 26543-28161/com.mytestapp D/XXXX: Iteration: 3 Result: 94240
08-18 13:10:21.152 26543-28161/com.mytestapp D/XXXX: Iteration: 4 Result: 93676
08-18 13:10:21.252 26543-28161/com.mytestapp D/XXXX: Iteration: 5 Result: 91554
08-18 13:10:21.352 26543-28161/com.mytestapp D/XXXX: Iteration: 6 Result: 94358
08-18 13:10:21.452 26543-28161/com.mytestapp D/XXXX: Iteration: 7 Result: 90954
08-18 13:10:21.552 26543-28161/com.mytestapp D/XXXX: Iteration: 8 Result: 94874
08-18 13:10:21.652 26543-28161/com.mytestapp D/XXXX: Iteration: 9 Result: 94464
如果我等待几分钟再试一次,结果会再次增加到正常值。
为什么会有这种行为?如何避免它并始终获得正确的基准测试结果?
这是我的示例代码:
public void benchmarkIterator(){
int result = 0;
int iterations = 100;
for (int i=0; i<iterations; i++){
result = benchmark();
Log.d("XXXX", "Iteration: "+i+" Result: "+result);
}
}
....
public int benchmark(){
long startTime = System.currentTimeMillis();
int count=0;
double aux=0;
while((System.currentTimeMillis()-startTime)<100){
count++;
double d = 7777777777d;
aux = 0;
aux=aux+(int)(aux+Math.sin(d)*Math.cos(d));
}
return count;
}
【问题讨论】:
-
只是确认我的结论,更大的结果意味着更快的 CPU 对吗?我的意思是在这 100 毫秒中进行了更多的数学运算?可能是一些优化或 JIT 的东西吗?
-
结果越高,数学计算最快的就是这个cpu
-
会不会是过热和某种热节流?
标签: java android math benchmarking computation