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: "Scott Ellsworth" <email@hidden>
- Date: Mon, 27 Nov 2006 09:10:31 -0800
On 11/27/06, Martin <email@hidden> wrote:
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 ?
As a rule, I try to measure rather than guess. Shark can give you a
pretty good idea of where your time is going, and upcoming
dtrace-based tools can tell you more.
Your expectation should be that the method call does not matter. You
have the overhead of a single count method call, as compared with the
loop body. That expectation could be violated if, for example, you
implemented an NSLinkedListArray, with a count method that was order
n, instead of constant time.
Alternatively, eliminate the call via
int max = [array count];
for (int i=0; i<max; i++){
// do stuff
}
then time the two implementations.
For what it is worth, I have very rarely found microchanges like this
to matter much. Big wins come from algorithm changes, more often than
not.
Scott
_______________________________________________
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