Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which one will execute Faster Enhanced for loop or For loop?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Please can any body tell the performance of enhanced for loop and for loop in execution
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65228
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot imagine that there's much in the way of any difference. Use whichever one makes the most sense in the context of your code.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11945
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds like a case of premature optimization. You are almost always better off developing good readable (and therefore maintainable) code without concerning yourself too much about which of several different methodologies is theoretically better. Later, once your code is working to your satisfaction, if you find there is a performance issue, you should use a profiler to determine where the performance issue lies, and fix the specific problem identified by the profiler.

One of the big issues with this particular question is that it is difficult to provide any definitive answer, and even if one were provided, the Java bytecode compiler, or the JIT compiler could have enough differences on various computers that the result could not be guaranteed. And even if it were limited to just a single architecture and operating system, there is still the risk that any future upgrades to Java on that machine could render any statement meaningless.

To get around some of these sorts of issues in general (not just in Java), you can take a step back from most algorithms and consider how complex your code is, and one way of describing that is by using Big O notatation. However at a simplistic level, the complexity for both the enhanced for loop and the standard for loop are the same: O(n).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1.

When you consider you've probably already spent more time wondering which is better than you will ever save by using the 'faster' one, I'd let it go.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Monkhouse wrote:However at a simplistic level, the complexity for both the enhanced for loop and the standard for loop are the same: O(n).


Unless, of course, you totally screw up! For example, this loop has complexity O(n^2), which is WAY slower:



What's that you say -- you don't see what's different about it? Well, "mylist" is a LinkedList, and I'm using get(int) to access the elements, which is an O(n) operation, making the whole loop O(n^2)! This is a case where naive use of an "old" for-loop is far slower than using an enhanced loop, which will use an iterator. >
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic