Speck
Speck

Reputation: 11

AS3 - Does the amount of code in an object matter for multiple instances?

What's better?

1000 objects with 500 lines of code each

vs

1000 objects with 30 lines of code each that delegate to one manager with 500 lines of code.


Context:

I have signals in my game. Dozens and dozens of them. I like that they have better performance than the native Flash Event.

I figured that as I was going to have so many, they should be lightweight, so each signal only stores a few variables and three methods.

Variables: head, tail, isStepping, hasColdNodes, signalMgr

Methods: addListener, removeListener, dispatch


But all these signals delegate the heavywork to the signalMgr, as in:

signalMgr.addListener(this, listener, removeOnFirstCallback);

That manager handles all the doublylinked list stuff, and has much more code than a signal.

So, is it correct to think this way? I figure that if I had all the management code in the signal, that would be repeated in memory every time I instance one.

Upvotes: 0

Views: 59

Answers (2)

BotMaster
BotMaster

Reputation: 2223

In the context of your question this is pretty much irrelevant and both cases should not produce much difference.

From what you say it seems that you assume many things but did not actually check any. For instance when you say: "I like that they have better performance than the native Flash Event." I can only assume that you did read that somewhere but never try to verify it by yourself. There's only a few cases where using the signal system can make a tiny bit of difference, in most cases they don't bring much, in some cases they make things worse. In the context of Flash development signals do not bring anything more than simple convenience. Flash is an event driven system that cannot be turned off so using signals with it means using event + signals, not using signals alone. In the case of custom events using delegation is much more efficient and easier to use and doesn't require any object creation.

But the real answer to the question is even more simple: There's no point optimizing something that you don't know needs optimization. Even worse different OS will produce different optimization needs so anyone trying to answer a general optimization question can only fail or pretend to know.

Upvotes: 1

Denis Kokorin
Denis Kokorin

Reputation: 895

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. (c) DonaldKnuth

The ultimate goal of any programmer is to write clean code. So you should write clean code with a only a few thoughts of optimizations.

Upvotes: 0

Related Questions