Now this is a classic question. One of the strange things you see just learning Cocoa/Objective-C is this thing called the Enumerator loop which looks something like
NSArray *anArray = // ... ; NSEnumerator *enumerator = [anArray objectEnumerator]; id object; while ((object = [enumerator nextObject])) { // do something with object... }and naturally people ask if using NSEnumerator is really faster than using a for loop like this example
NSMutableArray *my array = //... int i; for( i=0; i < LIMIT ; i++) { NSManagedObject *obj = [myArray objectAtIndex:i]; //do something with object }The answer to this question is yes just a little bit though. I've been told by some smart people (though I haven't verified) that the kernel XNU on Mac OS X is optimized for Objective-C messages, even if you don't believe that we all know Apple optimizes so much of their own software to take advantage of Altivec(PPC), hardware accelleration, etc. So naturally you should assume Apple has given you some reason to use this loop. To test what is really faster I went to CocoaDev and grabbed code off a page where a discussion on this very topic took place (take a good look at the source code.) I compiled this code myself and then ran it once, cleared out the test and ran the test 3 times, all tests using 10,000 objects and 10,000 iterations then punched in the results in NeoOffice. Here are the results (all times are in seconds): (pay attention to the NSEnumerator and for loop tests (first 2 on the left in the graph) for now)
6 comments:
You should note to readers that Apple documentation advises against using NSEnumerators on mutable collections if the collection is going to be modified:
From the documentation:
Note: It is not safe to modify a mutable collection while enumerating through it. Some enumerators may currently allow enumeration of a collection that is modified, but this behavior is not guaranteed to be supported in the future.
You've answered this question only for arrays with 10,000 objects. If you rerun these tests with different numbers of objects, you'll find that the NSEnumerator and for-loop timing differs between small and large arrays.
The numbers I've seen -- for years -- suggest that for-loops are far superior for most arrays (which are 'small').
Also, if you were at the WWDC session where they discussed ObjC 2.0 in-depth, you'd have wanted to pay special attention to the section about collection iteration.
You've answered this question only for arrays with 10,000 objects. If you rerun these tests with different numbers of objects, you'll find that the NSEnumerator and for-loop timing differs between small and large arrays.
The numbers I've seen -- for years -- suggest that for-loops are far superior for most arrays (which are 'small').
Also, if you were at the WWDC session where they discussed ObjC 2.0 in-depth, you'd have wanted to pay special attention to the section about collection iteration.
Sorry for the dupe, I meant to sign in. :)
Thanks for posting this test, I was wondering if I could/should use a for loop - I'm just so darn accustomed to them.
Stepping through arrays in the classic way array[0] etc allows you to go forward, backward and to use random access.
NSEnumerator only allows forwards.
Seems like the only thing you get is a marginal improvement in speed.
Post a Comment