• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

Please can any body tell the performance of enhanced for loop and for loop in execution
 
Sheriff
Posts: 67745
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. >
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic