2

I have been trying to evaluate the performance of for loop.

I had a look at this and this.

But I have not yet understood what is the correct way to measure the performance of for loops.

Should we also consider inserting some elements in the data structure like ArrayList?

There is another link which also say something about it.

Community
  • 1
  • 1
Sam
  • 2,352
  • 4
  • 32
  • 45
  • possible duplicate of [Java for loop performance question](http://stackoverflow.com/questions/2383422/java-for-loop-performance-question) – Rong Nguyen Sep 11 '13 at 05:32
  • 2
    I think the web links tha that you have provided clearly evaluates the performance of for loop in java language. What more do you want? In my opinion, trying to achieve perfection all times, may not be that beneficial. What do you want to achieve by evauluating its performance. Do you have any code base which has several nested for loops which you are trying to eliminate to improve? Post some code. – MansoorShaikh Sep 11 '13 at 05:33
  • @ MansoorShaikh I am not exactly help up with the code base which has some nested for loops. The only thing I am not able to find is do really JIT optimizes on the basis which style of for-loop we write. I was not not really successful to find out what happens under the hood. So I have posted the question. – Sam Sep 11 '13 at 05:37
  • @RongNK I have gone through such several posts before but no one actually proved exactly which is faster and why. – Sam Sep 11 '13 at 05:40

1 Answers1

3

Use the syntax that most clearly expresses what you're trying to do. The actual for loop conditions probably won't be a significant performance factor (you can test if you think they really are), and it's more important that the code be readable.

One guideline is to avoid known expensive methods inside the condition; collection.size() is a notable one here. When iterating over a collection, using an Iterator (either explicitly or via the enhanced for loop) usually makes for clearer code anyway.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Absolutely. It's exactly equivalent to `T t; Iterator it = collection.iterator(); while(it.hasNext() { t = it.next(); ...` – chrylis -cautiouslyoptimistic- Sep 11 '13 at 05:42
  • Agreed that the for-each version is simpler and, cleaner. But unless I'm misunderstanding what you're saying, your "known expensive method" example is sort of bogus. There's no _guarantee_ collection.size() will be fast, but the most common Collections (LinkedList, ArrayList, TreeMap, HashMap) all implement size() as an O(1) variable fetch. – James Sep 11 '13 at 05:54
  • 1
    The regular collections do cache the size, but collections backed by external results (e.g., a JPA one-to-many) can be a lot more expensive, and it's nearly always better to use the `Iterator` anyway, so we don't get more questions about `ConcurrentModificationException`. – chrylis -cautiouslyoptimistic- Sep 11 '13 at 05:57
  • @chrylis - Fair enough. – James Sep 11 '13 at 06:37