• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Should I do loops in reverse order ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Should I do loops in reverse order ?
      • From: Bill Bumgarner <email@hidden>
References: 
 >Should I do loops in reverse order ? (From: Martin <email@hidden>)

  • Prev by Date: Re: Should I do loops in reverse order ?
  • Next by Date: Re: Should I do loops in reverse order ?
  • Previous by thread: Re: Should I do loops in reverse order ?
  • Next by thread: Re: Should I do loops in reverse order ?
  • Index(es):
    • Date
    • Thread