1

Is there a difference in terms of efficiency between

int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
   System.out.println("Count is: " + item);
}

and

int[] numbers = {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < 10; i++) {
   System.out.println("Count is: " + numbers[i]);
}
John
  • 4,596
  • 11
  • 37
  • 43

5 Answers5

5

They are the same for arrays. If you have an orders list of numbers you should use

for(int i = min; i <= max; max += stride)

or

for(int i = min; i < max+stride; max += stride)

e.g.

for(int i = 1; i < 11; i++)

or

for(int i = 1; i <= 10; i++)

or if incrementing by 10

for(int j = 0; j <= 100; j += 10)

if you a predefined list of values to use, you can use an array.

I often write them like this

for (int item : new int[] {1, 2, 5, 10, 20, 50, 100}) 
   System.out.println("Count is: " + item);
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 3
    -1. Sorry, IMO, his style should only be used if the values are not in order. For i=1..10 style loops have been a standard and successful language idiom for longer than many of us have been alive. In your style, a support programmer has to read all 10 ints to check if these are in order. – user949300 Oct 22 '12 at 23:55
  • @user949300 Good point, using an array is not a good idea if you have a sequence. – Peter Lawrey Oct 23 '12 at 10:06
  • I like your new example where the array is not a sequence but a "vaguely logarithmic" series of numbers. So I'll undo the -1. – user949300 Oct 23 '12 at 14:24
  • @user949300 I use "vaguely logarithmic" searches for parameter optimisation quite a bit these days. – Peter Lawrey Oct 23 '12 at 15:58
3

Actually, the bytecode generated for both of your code snippets will be almost exactly the same. You can check this with tools like javap, or even view it in Eclipse's decompiled class viewer.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 1
    Only for arrays. For `Collection`s they are completely different. – Steve Kuo Oct 22 '12 at 20:07
  • 1
    @SteveKuo There are two pieces of code in OP's question. The generated bytecode will be almost exactly the same. Your comment makes little sense. – Marko Topolnik Oct 22 '12 at 20:09
  • Let me be more clear. For a `Collection`, indexing vs iterator can be vastly different, depending on the collection implementation (such as `LinkedList`). – Steve Kuo Oct 22 '12 at 20:15
  • I think SteveKuo is trying to say that although the generated code in this particular example would be very similar, if instead of being an array the structure being traversed was, say, a LinkedList, then the two would be different. He's not saying that your answer is wrong, just adding a point of potential interest. – Jay Oct 22 '12 at 20:15
  • @SteveKuo That is true, but I still don't see the point of bringing it up for this particular question. Iterating over a `List` by indexing is bad practice, anyway, so I don't assume OP or anyone else would write it like that. – Marko Topolnik Oct 22 '12 at 20:19
3

For Array [], its similar but for Collections such as ArrayList, it will be different.

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

Unless you are doing something extremely strange inside the loop, the difference in performance will be undetectable. Whatever the answer to the question, this is a "penny wise, pound foolish" optimization. The decision should be made on what capabilities you need and which code is clearer. (IMO, Option 1 is simpler and clearer, though sometimes you need Option 2)

You have probably spent more time typing the question and reading answers than the added time spent in the loop for the life of your program. :-)

Not sure where to ask this, but I think there should be some "premature optimization" tag we can add to questions like this one.

user949300
  • 15,364
  • 7
  • 35
  • 66
0

It will be very similar in performance so is up to you choose your favorite. For me a for-each is always more elegant but each programmer is different ^^

David Moreno García
  • 4,423
  • 8
  • 49
  • 82