English 中文(简体)
AS3: Faster to use EventListeners or ArrayLoops?
原标题:

I am writing a game in AS3 and have, as an example, around 40 objects on screen. Let s say they are clouds. I m wondering which of the two paths would be less a strain on system resources:

a) Put an eventListener on each object and have it update itself, or

b) Loop through the array and manually update each object

Or is the performance difference negligable? Does either solution scale better than the others?

最佳回答

This is a tricky one, purists would say to to go with the array looping method, as with this method it would be possible to cut out the view out of the MVC and still have the system working (which is a good test of any system). However, if you are working with the events you can cut some corners with event bubbling and strict typing. For example if we assume you make a custom event called CloudEvent that has a property called cloud that contains a reference to the dispatching CloudSprite, then as long as the event bubbles by default, you don t need to add an event listener to each one, just to the DisplayObjectContainer which holds them (which I am imaginatively calling CloudContainer). This way the event bubbles up and you only have to add one listener, and don t have to worry about managing listeners on child items.

public function CloudContainer()
{
    super();
    addEventListener(CloudEvent.CHANGE, cloudChangeHandler);
}

private function cloudChangeHandler(evt:CloudEvent):void
{
    var cloud:CloudSprite = evt.cloud;
    cloud.update();
}

Hope this helps

问题回答

I would expect the performance to be fairly negligable either way. Once you get a lot of objects you might see a difference (with the loop being the winner). From my experience Adobe put a lot of work into optimizing the actionscript event listener path.

I am under the impression that event listeners require more resources so it is better to use an array when performance is required. It would also scale better for that reason. If you are using Flash 10 then use a Vector, I believe it offers better performance than an array (and type saftey).

Use EventListeners! Just make sure you manage them properly ie. remove them when you done, use weak references.

You wont really find much performance from doing things like this. Usually better performance comes from the bigger ticked items, like not using filters, lowering the frame-rate etc. So punishing your code clarity and OO for the sake of a half a millisecond is not worth it in my book.

There are a few really great guides out there that will teach you all about optimizing in AS3. The best one I have found is Grant Skinner s; AS3 Resource Management. And I just found a quicker seven step version. I would definitely recommend everyone doing AS3 should read the Grant Skinner slides though.

Of course don t just take my word (or anyone else answering your question), you can do you own tests and actually see whats up using a profiler. See Lee Brimlow s latest video tutorial for how to do this. It is worth the watch! Check it out here: GotoAndLearn - ActionScript 3 Performance Testing.





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签