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: John Stiles <email@hidden>
- Date: Mon, 27 Nov 2006 09:18:03 -0800
There's a ton of discussion on Cocoadev that you could read:
http://www.cocoadev.com/index.pl?IteratingThroughAnArray
I found it to be a bit light on actual results though, just programmers
analyzing loop structures to death :)
In general I think your method B will probably be faster, but I doubt it
will be a measurable/significant difference. If your loop body is doing
real work, it should dwarf that one extra Objective-C call. So it is
probably just academic.
You might consider using NSEnumerator; that will let you minimize the
number of Objective-C calls you make while still walking the loop in a
clean, object-oriented way (not to mention walking forwards instead of
backwards). Then again I don't know how NSEnumerator works internally
so, while I doubt it's the case, theoretically it might take more cycles
than [myArray count] plus [myArray objectAtIndex:i]. The only way to
know for sure is to Shark it, if you're curious.
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
}
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").
Thanks,
Martin.
_______________________________________________
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
_______________________________________________
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