开发者

Java Loop Efficiency. if-continue OR if

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开发者_StackOverflow++){
    if(arrExample[idx] != 'x')
        System.out.println(arrExample[idx]);
}

so.. expert comments are pleased. Thanks!


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.


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.


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


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.


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.


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.


The complexity of both loops are equal, none of them will give you a significant differences. I, personally, don't want to sacrifice code readibility just to save some microseconds.

Let's say you choose one of the above implementations. Different compiler will produce different optimization and your 'microseconds saving' may be gone.

In terms of code readability, I prefer the one without 'continue' or 'break'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜