Re: loop efficiency & messages
Re: loop efficiency & messages
- Subject: Re: loop efficiency & messages
- From: Pandaa <email@hidden>
- Date: Wed, 23 Mar 2005 07:36:45 +0100
That has no effect whatsoever. The below generates the exact same
object code as for ( int i = 0 ; i < [self getCount] ; i++ ). ( check
it yourself )
2005-03-23 kl. 00.32 skrev Mont Rothstein:
I am jumping in the middle of this thread here (without having read
the previous posts), so I apologize if I am covering ground already
covered.
In the two below snipits of code change the second to the following to
make them equivalent:
for (i = 0; i < (const int)[self getCount]; i++)
-Mont
On Mar 22, 2005, at 3:20 PM, Thomas Davie wrote:
On Mar 22, 2005, at 7:57 PM, Mark Dawson wrote:
if I have a getter: -getCount { return mCount; }
How much of a difference are there between the following two loops?
int count = [self getCount];
for (i = 0; i < count; i++)
…
for (i = 0; i < [self getCount]; i++)
Does compiler optimize this similar to a C++ inline so that count is
just stored in a register, or does a message sequence happen each
time? If messages happen each time, is it a better habit to do the
first for loop (where the compiler should just keep count in a
register)? I realize that it may not make much difference with most
loops, but if there's a "better" way, I'd like to get into the habit
of typing the "better" code…
The simple answer is that the compiler will not optimize that, and in
fact the reason is that they are semantically different, so the
compiler would be introducing a bug if it optimized one into the
other... Just consider if there were another thread running that
added items to the array... The effect is different with the two
pieces of code.
Bob
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. email@hidden . . www.synapticpulse.net .
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden