aspose file tools*
The moose likes Beginning Java and the fly likes Which one will execute Faster Enhanced for loop or For loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Which one will execute Faster Enhanced for loop or For loop?" Watch "Which one will execute Faster Enhanced for loop or For loop?" New topic
Author

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

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear All,

Please can any body tell the performance of enhanced for loop and for loop in execution


Creativity is nothing but Breaking Rules
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61193
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11460
    
  94

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).


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11308
    
  16

+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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

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. >


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which one will execute Faster Enhanced for loop or For loop?