3

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.

4
  • 1
    You can do exactly the same thing with both loops. eg:: you could break the for loop once duv <= delta. Commented Nov 18, 2011 at 13:33
  • What units are you using for your time measurements? Milliseconds? If so, I think you're looking at a microbenchmark problem. Commented Nov 18, 2011 at 13:39
  • I agree, my question is why the time is almost the same as the while loop as it is doing much more iterations. Why when I put a simple counter It becomes more "normal"? Commented Nov 18, 2011 at 14:14
  • But, I guess I should have a look about the java benchmarking and trick as mentionned by pavel_kazlou. Commented Nov 18, 2011 at 14:15

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

@Aurélien updated answer - have a look at quick start if you get bored by excessive theory ;)
Thanks a lot, that cannot be simpler!
@Aurélien so have you performed the tests using library? what are the results?
1

You do not have the same termination condition. For the while loop it's:

duv > delta

and for the for loop it's

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.

1 Comment

Yes, exactly, so why the for loop is almost faster than the while loop?
0

When duv>delta the while-loop stops, but the for-loop continues. Both get the same result, but for continues checking. You should modify the for-loop like this: if (duv > delta) { execute(); } else break;

1 Comment

You didn't read the question correctly. The for loop is actually faster than the while loop, even though it performs more interations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.