Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. With a while loop with the condition as while parameter, the loop will stop once condition false and should save some iterations.

However, the while loop remains a little slower than the for loop!

But, if I put a int value as counter, and count iterations, the For loop as expected performed much more iterations. 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!

Any explanations?

here the code with a for loop:

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
time execution with a counter: 8299
iterations counted: 2584401

here the code with the while loop:

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
time execution with a counter: 7163
iterations counted: 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. Method getDistance() is just a matrix int[][] access.

Thanks for any help.

like image 645
Aurélien Avatar asked Sep 28 '26 00:09

Aurélien


1 Answers

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

In a few words - when running for some time Hotspot-enabled JVM can optimize your code which will affect the results of tests. 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.

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
  2. Put them on classpath
  3. Rewrite your code so that while loop approach and for loop approach both go in separate implementations of Runnable or Callable interface
  4. In your main method just invoke

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

to show execution time for while-loop and obviously

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

to get info for for-loop

like image 114
pavel_kazlou Avatar answered Sep 30 '26 13:09

pavel_kazlou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!