Re: loop efficiency & messages
Re: loop efficiency & messages
- Subject: Re: loop efficiency & messages
- From: Ondra Cada <email@hidden>
- Date: Tue, 22 Mar 2005 21:49:52 +0100
Mark,
On 22.3.2005, at 20:57, 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++)
Well, a message dispatch. Pretty quick thing, unless this particular
code is a real bottleneck (or getCount itself happens to be really
slow) don't care. Premature optimization's a bitch.
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?
Very bad idea. If it did this "optimization", it would be a *very grave
bug*. There's no way the compiler can ever know the value returned
would not change the next time the message is sent.
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 better way is the one which in the concrete case happens to be more
human-readable and better maintaintable. The speed issues are
*COMPLETELY* negligible (unless, as written above, the getCount itself
is really slow).
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
_______________________________________________
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