我有一个简单的问题
我正在循环一段代码,这段代码的目的是收集所用的时间。我可以在每次运行循环时执行此操作,但我想要做的是,例如,收集每次运行循环的持续时间,存储这些值,将它们加在一起,然后对这些值求平均值我实现了这个吗?
此外,当循环完成其次数后,它会继续前进,并且需要重置总持续时间
这是我当前的代码:
if (myrank == 0) {
for (int len = minlen; len <= maxlen; len *= 2) { //len=*2 doubles the ping size each time
for (int i = 0; i < MAX_LOOPS;) {
long startTime = System.nanoTime();
long endTime = System.nanoTime();
long duration = endTime - startTime;
durationseconds = duration; // this is where i want to store all the durations
i++;
}
谢谢大家:)
最佳答案
你可以有像下面这样的东西:
if (myrank == 0) {
for (int len = minlen; len <= maxlen; len *= 2) {
long durationseconds =0;
for (int i = 0; i < MAX_LOOPS;) {
----
durationseconds = durationseconds + duration;
---
}
float avgSecPerLoop = durationsseconds / MAX_LOOPS
关于java - 如何在循环中向变量添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16041039/