简体   繁体   English

Java For循环vs While循环,奇怪的行为和时间性能

[英]Java For loop vs While loop, strange behavior and time performance

I'm writing an algorithm which do a big loop over an integer array from the end to the beginning with a if condition inside. 我正在编写一个算法,它在整个数组中从一个整数数组做一个大循环,内部有一个if条件。 At the first time the condition is false the loop can be terminated. 在第一次条件为假时,可以终止循环。

So, with a for loop, if condition is false it continues to iterate with simple variables changes. 因此,对于for循环,如果condition为false,则继续使用简单变量进行迭代。 With a while loop with the condition as while parameter, the loop will stop once condition false and should save some iterations. 使用while循环,条件为while参数,循环将在条件为false时停止,并应保存一些迭代。

However, the while loop remains a little slower than the for loop! 但是,while循环仍然比for循环慢一点!

But, if I put a int value as counter, and count iterations, the For loop as expected performed much more iterations. 但是,如果我将一个int值作为计数器,并计算迭代次数,For循环按预期执行了更多的迭代。 However this time, the time execution of the mofified For method with the counter will be much more slower than the while method with a counter! 但是这次,使用计数器执行mofified For方法的时间比使用计数器的while方法慢得多!

Any explanations? 有什么解释吗?

here the code with a for loop: 这里带有for循环的代码:

for (int i = pairs.length - 1; i >= 0; i -= 2) {
    //cpt++;
    u = pairs[i];
    v = pairs[i - 1];

    duv = bfsResult.distanceMatrix.getDistance(u, v);

    if (duv > delta) {
        execute();
    }
}

time execution: 6473 时间执行:6473
time execution with a counter: 8299 用计数器执行时间:8299
iterations counted: 2584401 迭代计数:2584401

here the code with the while loop: 这里带有while循环的代码:

int i = pairs.length - 1;

u = pairs[i];
v = pairs[i - 1];

duv = bfsResult.distanceMatrix.getDistance(u, v);

while (duv > delta) {
    //cpt++;
    execute();

    u = pairs[i -= 2];
    v = pairs[i - 1];
    duv = bfsResult.distanceMatrix.getDistance(u, v);
}

time execution: 6632 时间执行:6632
time execution with a counter: 7163 用计数器执行时间:7163
iterations counted: 9793 迭代计数:9793

Time is in ms, I repeated the experiment several times with different size intances, the measures remained almost the same. 时间以毫秒为单位,我用不同大小的内容重复实验几次,措施几乎保持不变。 The execute() method updates the delta value. execute()方法更新delta值。 Method getDistance() is just a matrix int[][] access. 方法getDistance()只是一个矩阵int [] []访问。

Thanks for any help. 谢谢你的帮助。

Before you try to perform any performance tests on java I highly recommend you reading this article http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html 在尝试对java执行任何性能测试之前,我强烈建议您阅读本文http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html

In a few words - when running for some time Hotspot-enabled JVM can optimize your code which will affect the results of tests. 简而言之 - 运行一段时间后,支持Hotspot的JVM可以优化您的代码,这将影响测试结果。 So you need proper technique to test performance of your code. 因此,您需要使用适当的技术来测试代码的性能。 To ease the pain there is a library used for performing proper tests: http://ellipticgroup.com/html/benchmarkingArticle.html You can find links to both parts of the article on this page. 为了减轻痛苦,有一个用于执行正确测试的库: http//ellipticgroup.com/html/benchmarkingArticle.html您可以在此页面上找到本文两部分的链接。

Update: to help you start quicker with this here is what you just need to do: 更新:为了帮助您更快地开始这里,您需要做的就是:

  1. Download bb.jar, jsci-core.jar, mt-13.jar found on the page 下载bb.jar,jsci-core.jar添加,MT-13.jar在上找到网页
  2. Put them on classpath 把它们放在classpath上
  3. Rewrite your code so that while loop approach and for loop approach both go in separate implementations of Runnable or Callable interface 重写您的代码,以便while循环方法和for循环方法都进入Runnable或Callable接口的单独实现
  4. In your main method just invoke 在你的main方法中只需调用

System.out.println(new Benchmark(new WhileApproach()));

to show execution time for while-loop and obviously 显示while循环的执行时间

System.out.println(new Benchmark(new ForApproach()));

to get info for for-loop 获取for循环的信息

You do not have the same termination condition. 您没有相同的终止条件。 For the while loop it's: 对于while循环,它是:

duv > delta

and for the for loop it's 而对于for循环来说

i >= 0

The two scenarios are not equivalent. 这两种情况不相同。 My guess is that the while loop condition becomes false way sooner than the for condition and therefore it executes less iterations. 我的猜测是,while循环条件比for条件更快地变为false,因此它执行的迭代次数更少。

When duv>delta the while-loop stops, but the for-loop continues. duv>delta ,while循环停止,但for循环继续。 Both get the same result, but for continues checking. 都得到相同的结果,但for继续检查。 You should modify the for-loop like this: if (duv > delta) { execute(); } else break; 您应该像这样修改for循环: if (duv > delta) { execute(); } else break; if (duv > delta) { execute(); } else break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM