Re: Instance vars, local vars and speed
Re: Instance vars, local vars and speed
- Subject: Re: Instance vars, local vars and speed
- From: "Michael Ash" <email@hidden>
- Date: Mon, 4 Aug 2008 10:26:34 -0400
On Mon, Aug 4, 2008 at 7:26 AM, Trygve Inda <email@hidden> wrote:
> I am transitioning some code from Carbon to Cocoa and redesigning a lot of
> how things work. I am doing a bit of low-level pixel manipulation and
> wondering about speed of access to variables declared in a method vs those
> declared at the object level.
>
> Obviously things like x,y loop counters are local to a method, but some
> things need to be shared between methods and rather then passing values
> around, it would be cleaner to declare these at the object level.
>
> How much of a hit am I going to take?
If you can't answer this question yourself by measuring it, you
shouldn't be asking it yet.
Rules of Optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet.
Particularly when dealing with this sort of nitpicky low-level
optimization, it is impossible to start from scratch and write code
that is both fast and functional. (As opposed to code which is totally
broken, but which is really fast at being totally broken.) If you
start out by trying to write fast code, you'll end up with code that
doesn't work, probably isn't all that fast, and is so difficult to
work with that you have no hope of making it correct.
So the proper approach is to build code that's simple,
straightforward, and functional. Then if it's not fast enough, run a
profiler on it to see what parts of it are slow. (This is the part
that will tell you how much of a hit you'll take from using instance
variables.) Then fix those parts to make them faster. If it turns out
that using instance variables is hurting you a lot, at this point you
can port them across to local variables to gain some speed.
Note that the above advice doesn't apply to everything. If there is
some sort of design or grand algorithmic optimization to be made, it's
often good to do it from the start. But when it comes down to
low-level stuff, if you're doing it before you have written functional
code, you're doing it too early. You say that this question affects
your overall design, but it really shouldn't. If ivars are slow, you
just move them into local variables before the beginning of your loop.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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