Cocoa/Objective-C: how much optimization should I do myself? <p>I recently found myself writing a piece of code that executed a Core Data fetch, then allocated two mutable arrays with the initial capacity being equal to the number of results returned from the fetch:</p> <pre><code>// Have some existing context, request, and error objects NSArray *results = [context executeFetchRequest:request error:&error]; NSMutableArray *firstArray = [[[NSMutableArray alloc] initWithCapacity:[results count]] autorelease]; NSMutableArray *secondArray = [[[NSMutableArray alloc] initWithCapacity:[results count]] autorelease];</code></pre> <p>I looked this over again once I'd written it, and something struck me as odd: I was calling <code>[results count]</code> twice. The resultset is potentially pretty large (hundreds, maybe a thousand, objects).</p> <p>My first instict was to break out <code>[results count]</code> into a separate NSUInteger, then use that integer for the capacity of each of the arrays. My question: is this kind of by-hand optimization necessary? Will the compiler recognize it's running <code>[results count]</code> twice and just hold the value without me having to explicitly specify that behavior? Or will it run the method twice - a potentially costly operation?</p> <p>In a similar vein, what other optimizations should programmers (especially iPhone programmers, where there's a limited amount of memory/processing power available) do by hand, as opposed to trusting the compiler?</p>