views:

97

answers:

6

Hi.

The question being thought today while completing my task is about the loop efficiency..

Lets say. I have an array {'a','x','a','a'} And a loop that should avoid 'x' element Now i wonder how these two approach differ in term of efficiency - not taking the while or do-while or for loops differentiation

char[] arrExample = {'a','x','a','a'};
for(int idx = 0; idx<arrExample.length; idx++){
    if(arrExample[idx] == 'x')
        continue;

    System.out.println(arrExample[idx]);
}

and the common loop look like this

char[] arrExample = {'a','x','a','a'};
for(int idx = 0; idx<arrExample.length; idx++){
    if(arrExample[idx] != 'x')
        System.out.println(arrExample[idx]);
}

so.. expert comments are pleased. Thanks!

+3  A: 

An optimizing compiler will generate essentially identical instructions for both. Use whichever makes more sense to you.

Amber
@Stephen: I changed the wording to make it a bit more generic; the details aren't really relevant to the message. :)
Amber
Thanks for the adivse!
ommar
+3  A: 

I would say that if you are concerned with that you are looking at way too low of a level. Odds are that there is no appreciable difference.

My preference for the coding would be generally not use continue and use the != instead as, it tends to lead to nicer code... but that isn't always true.

Prefer code clarity/nicety to speed unless speed proves to be a problem.

TofuBeer
Thanks for the adivse!
ommar
+1  A: 

If you really, really care about this loop's efficiency, then you have two choices as I see it:

  • Measure it (on the platform that you care about)
  • Check the bytecode that is generated to see which one is more efficient.

But really, as Amber pointed out, you should get identical byte codes with a semi-decent compiler. So use whatever feels clearer. For me, especially with a slightly larger body for the if, a continue makes it clearer.

Rohith
Checking the bytecodes isn't the whole answer. It is possible that the two programs result in different bytecodes, but the JIT compiler compiles the different bytecodes to identical native code.
Stephen C
Thanks for the adivse!
ommar
+2  A: 

Unless you have profiled your application on real inputs, and determined that this particular loop takes a significant amount of time, you should not be worrying about hand optimizing your code at this level:

  • The chances are that this particular loop is not significant to overall performance.
  • The chances are that the change you are considering won't make any difference.
  • Even if it did, there's a chance that the most efficient code will depend on the hardware platform and the JVM version. (The JIT compilers are constantly being improved.)

In fact, it has been stated by Sun folks that by writing your code in a complicated way, you can actually make it hard for the JIT optimizer to do a good job. So you while some hand optimization might improve performance with one JVM version, the same optimization could actually make performance worse with another version.

Stephen C
Thanks for the adivse!
ommar
+4  A: 

Even if you went to a nonoptimized byte-code, it will be nearly identical. There's only two real differences. First is the placement of a jump statement, but that jump statement will still be executed the same number of times. Also, the == and != operators are both atomic operator, taking the same amount of time to execute both.

As said before, use whatever makes sense to you. But I'd have to admit, that continue statement version is rather awkward to read.

David Nguyen
Thanks for the adivse!
ommar
+2  A: 

Performance wise you don't see much difference, but the second one looks a lot readable.

Even if there is a (very minor) performance difference, I would go for the second.

Nivas
Thanks for the adivse!
ommar