• 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: Matt Neuburg <email@hidden>
  • Date: Mon, 27 Nov 2006 09:22:16 -0800
  • Thread-topic: Should I do loops in reverse order ?

On Mon, 27 Nov 2006 17:59:01 +0100, Martin <email@hidden> said:
>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
>}
>
>Is it much better and should I always do (as order doesn't matter
>here) the following thing...
>
>int i;
>for ( i=[myArray count]-1 ; i>=0 ; i-- ) {
> // do stuff
>}
>
>...in order to call only once [myArray count] instead of calling it
>at each loop ? Is it much faster ? Is there a difference if myArray
>is a NSArray or a NSMutableArray (in that case I also need to assume
>that I don't modify the array's count in "//do stuff").

What I would do is:

int i,u=[myArray count];
for (i=0; i<u; i++)

However, I hope you realize that you aren't supposed to use a for-loop at
all. You're supposed to use an NSEnumerator. :( m.

--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>



_______________________________________________

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: Michael Rothwell <email@hidden>
    • Re: Should I do loops in reverse order ?
      • From: "Sean McBride" <email@hidden>
  • Prev by Date: Re: (no subject)
  • 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