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: "Sean McBride" <email@hidden>
- Date: Mon, 27 Nov 2006 12:26:12 -0500
- Organization: Rogue Research
On 2006-11-27 17:59, Martin said:
>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").
It really depends. You might want to use Shark to see if the loop in
question is a bottleneck. Of course, loops often are bottlenecks and
eliminating a method dispatch per iteration can certainly help. You can
also do this if you prefer going 'forward' in the array:
unsigned count = [myArray count];
for ( unsigned i=0 ; i<count ; i++ ) {
// do stuff
}
And with Obj-C 2 in 10.5 you can use "fast enumeration".
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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