views:

1932

answers:

2

I know that if you have a loop that modifies the count of the items in the loop, using the NSEnumerator on a set is the best way to make sure your code blows up, however I would like to understand the performance tradeoffs between the NSEnumerator class and just an old school for loop

+1  A: 

They are very similar. With Objective-C 2.0 most enumerations now default to NSFastEnumeration which creates a buffer of the addresses to each object in the collection that it can then deliver. The one step that you save over the classic for loop is not having to call objectAtIndex:i each time inside the loop. The internals of the collection you are enumerating implement fast enumeration with out calling objectAtIndex:i method.

The buffer is part of the reason that you can't mutate a collection as you enumerate, the address of the objects will change and the buffer that was built will no longer match.

As a bonus the format in 2.0 looks as nice as the classic for loop:

for ( Type newVariable in expression ) { 
    stmts 
}

Read the following documentaion to go deeper:

file:///Developer/Documentation/DocSets
/com.apple.ADC_Reference_Library.CoreReference.docset/
Contents/Resources/Documents/documentation/Cocoa/
Reference/NSFastEnumeration_protocol/Reference/NSFastEnumeration.html

+6  A: 

Using the new for (... in ...) syntax in Objective-C 2.0 is generally the fastest way to iterate over a collection because it can maintain a buffer on the stack and get batches of items into it.

Using NSEnumerator is generally the slowest way because it often copies the collection being iterated; for immutable collections this can be cheap (equivalent to -retain) but for mutable collections it can cause an immutable copy to be created.

Doing your own iteration — for example, using -[NSArray objectAtIndex:] — will generally fall somewhere in between because while you won't have the potential copying overhead, you also won't be getting batches of objects from the underlying collection.

(PS - This question should be tagged as Objective-C, not C, since NSEnumerator is a Cocoa class and the new for (... in ...) syntax is specific to Objective-C.)

Chris Hanson