Re: Should I do loops in reverse order ?
Re: Should I do loops in reverse order ?
- Subject: Re: Should I do loops in reverse order ?
- From: Shawn Erickson <email@hidden>
- Date: Mon, 27 Nov 2006 09:19:13 -0800
On Nov 27, 2006, at 8:59 AM, Martin wrote:
Hi,
Let's say I'm looping through an array's objects, and, in that
particular case, order doesn't matter :
int i;
for ( i=0 ; i<[myArray count] ; i++ ) {
// do stuff
}
Don't forget ...
int count = [myArray count];
for (int i = 0; i < count; i++) { ... }
...or...
id object;
NSEnumerator* e = [myArray objectEnumerator];
while ((object = [e nextObject]) != nil) { ... }
...or using Objective-C 2.0 (you can glean the syntax from open
sourced code)...
for (id object in myArray) { ... }
Anyway it is hard to answer your question... it depends on the size
of the array and what "do stuff" does. The messaging over head of
calling count could be noise compared to the work done by "do stuff"
for example.
Often it is best to implement it in the most readable form and then
profile the application with representative data sets (early and
often) to see if you have any hot spots that need to be corrected.
-Shawn
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden