<p>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</p>

[{'answer_id': 33035, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>They are very similar. With Objective-C 2.0 most enumerations now default to <code>NSFastEnumeration</code> 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 <code>objectAtIndex:i</code> each time inside the loop. The internals of the collection you are enumerating implement fast enumeration with out calling <code>objectAtIndex:i method</code>.</p>\n\n<p>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. </p>\n\n<p>As a bonus the format in 2.0 looks as nice as the classic for loop:</p>\n\n<pre><code>for ( Type newVariable in expression ) { \n    stmts \n}\n</code></pre>\n\n<p>Read the following documentaion to go deeper:\n<a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSFastEnumeration_protocol/index.html" rel="nofollow noreferrer">NSFastEnumeration Protocol Reference</a></p>\n'}, {'answer_id': 33256, 'author': 'Chris Hanson', 'author_id': 714, 'author_profile': 'https://Stackoverflow.com/users/714', 'pm_score': 6, 'selected': True, 'text': "<p>Using the new <code>for (... in ...)</code> 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.</p>\n\n<p>Using <code>NSEnumerator</code> is generally the slowest way because it often copies the collection being iterated; for immutable collections this can be cheap (equivalent to <code>-retain</code>) but for mutable collections it can cause an immutable copy to be created.</p>\n\n<p>Doing your own iteration — for example, using <code>-[NSArray objectAtIndex:]</code> — 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.</p>\n\n<p>(PS - This question should be tagged as Objective-C, not C, since <code>NSEnumerator</code> is a Cocoa class and the new <code>for (... in ...)</code> syntax is specific to Objective-C.)</p>\n"}, {'answer_id': 30720339, 'author': 'Zsivics Sanel', 'author_id': 1600061, 'author_profile': 'https://Stackoverflow.com/users/1600061', 'pm_score': 3, 'selected': False, 'text': '<p>After running the test several times, the result is almost the same. Each measure block runs 10 times consecutively.</p>\n\n<p>The result in my case from the fastest to the slowest: </p>\n\n<ol>\n<li><strong>For..in</strong> (testPerformanceExample3) <strong>(0.006 sec)</strong></li>\n<li><strong>While</strong> (testPerformanceExample4) <strong>(0.026 sec)</strong></li>\n<li><strong>For(;;)</strong> (testPerformanceExample1) <strong>(0.027 sec)</strong></li>\n<li><strong>Enumeration block</strong> (testPerformanceExample2) <strong>(0.067 sec)</strong></li>\n</ol>\n\n<p>The for and while loop is almost the same.</p>\n\n<p><img src="https://i.stack.imgur.com/Arhu7.png" alt="comparation between iterations"></p>\n\n<p>The <code>tmp</code> is an <code>NSArray</code> which contains 1 million objects from 0 to 999999.</p>\n\n<pre><code>- (NSArray *)createArray\n{\n    self.tmpArray = [NSMutableArray array];\n    for (int i = 0; i &lt; 1000000; i++)\n    {\n        [self.tmpArray addObject:@(i)];\n    }\n    return self.tmpArray;\n}\n</code></pre>\n\n<p>The whole code:</p>\n\n<p>ViewController.h</p>\n\n<pre><code>#import &lt;UIKit/UIKit.h&gt;\n\n@interface ViewController : UIViewController\n\n@property (strong, nonatomic) NSMutableArray *tmpArray;\n- (NSArray *)createArray;\n\n@end\n</code></pre>\n\n<p>ViewController.m</p>\n\n<pre><code>#import "ViewController.h"\n\n@implementation ViewController\n\n- (void)viewDidLoad {\n    [super viewDidLoad];\n    [self createArray];\n}\n\n- (NSArray *)createArray\n{\n    self.tmpArray = [NSMutableArray array];\n    for (int i = 0; i &lt; 1000000; i++)\n    {\n        [self.tmpArray addObject:@(i)];\n    }\n    return self.tmpArray;\n}\n\n@end\n</code></pre>\n\n<p>MyTestfile.m</p>\n\n<pre><code>#import &lt;UIKit/UIKit.h&gt;\n#import &lt;XCTest/XCTest.h&gt;\n\n#import "ViewController.h"\n\n@interface TestCaseXcodeTests : XCTestCase\n{\n    ViewController *vc;\n    NSArray *tmp;\n}\n\n@end\n\n@implementation TestCaseXcodeTests\n\n- (void)setUp {\n    [super setUp];\n    vc = [[ViewController alloc] init];\n    tmp = vc.createArray;\n}\n\n- (void)testPerformanceExample1\n{\n    [self measureBlock:^{\n        for (int i = 0; i &lt; [tmp count]; i++)\n        {\n            [tmp objectAtIndex:i];\n        }\n    }];\n}\n\n- (void)testPerformanceExample2\n{\n    [self measureBlock:^{\n        [tmp enumerateObjectsUsingBlock:^(NSNumber *obj, NSUInteger idx, BOOL *stop) {\n           obj;\n        }];\n    }];\n}\n\n- (void)testPerformanceExample3\n{\n    [self measureBlock:^{\n        for (NSNumber *num in tmp)\n        {\n            num;\n        }\n    }];\n}\n\n- (void)testPerformanceExample4\n{\n    [self measureBlock:^{\n        int i = 0;\n        while (i &lt; [tmp count])\n        {\n            [tmp objectAtIndex:i];\n            i++;\n        }\n    }];\n}\n\n@end\n</code></pre>\n\n<p>For more information visit: <a href="https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/testing_with_xcode/Introduction/Introduction.html#//apple_ref/doc/uid/TP40014132-CH1-SW1" rel="noreferrer">Apples "About Testing with Xcode"</a></p>\n'}]
['https://Stackoverflow.com/questions/32986', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3484/']