开发者

flex performace, method calls or events

I'm trying to knock some cpu cycles off my app and I want to know what the least cpu intensive methods are for executing code.

Take these 2 examples. The first is an enterframe running and calling various methods. The second is the same enter frame but dispatching events instead.

1) Would it be likely for one of these enterframes to be less cpu intensive than the other?

2) In the first example does the frame have to wait until method 1 2 and 3 have been completed before the next frame can render and execute code?

3) In the 2nd example could the events dispatched to method 1 2 and 3 be executed in a frame other than the one that called it? EG, if the the cpu is under pressure can it defer the execution?

public function enterframe(e:Event):void
{
method1();
method2();
method3();
}

public function enterframe(e:Event):void
{
dispatchEvent(MethodEvent.Test, method1);
dispatchEvent(MethodEvent.Test, method2);
dispatchEvent(MethodEvent.Test, method2);
}
开发者_StackOverflow


I think the best you can do about optimization is forget enterFrame event. Place a Timer in your code, and set the delay according to your exact needs. You can experience a huge optimization easily.

As far as I know enterFrame is connected to the fps. So if your movie is a 30 fps animation, then it will run 30 times a second.

Now, this means it is more like an interval (flash.utils.setInterval), or a Timer.

Therefore you can see, enterFrame is more or less an easy way to setup an interval, or an interval-like functionality. If you know your needs, the refresh rate and stuff, you can set the timer and interval more precisely to gain a huge performance increase.


This is a classic case of premature optimization. Whether you use Event Driven approach or method call should be based on good design practices, not based on which one is faster.

In any case, Flex is mostly asynchronous and it maintains loose component coupling and event chaining, using event driven model and in post probability the methodology you are looking in to will not help substantial performance improvement (especially cpu cycles ??). I suggest that you use Flex Profiler, if you think your performance is not optimal. (mostly its storing components in memory, that slows down flex application. As long as you free obsolete component reference...)


Edit: Since I got confused with events dispatched by built in features in the Flash player and custom events dispatched by as3 code my first answer is incorrect. Although the flash player itself is multithreaded, you can't access threading functionality in as3. Here's my amended answer:

1) Would it be likely for one of these enterframes to be less cpu intensive than the other?

Well if you look at the amount of instructions the one dispatching a event have more instructions until it actually executes the code in the methods. It first have to run through a list of event listeners and look for the right one in that particular object compared from the given event type.

2) In the first example does the frame have to wait until method 1 2 and 3 have been completed before the next frame can render and execute code?

Yes. Same thing happens in the second example.

3) In the 2nd example could the events dispatched to method 1 2 and 3 be executed in a frame other than the one that called it? EG, if the the cpu is under pressure can it defer the execution?

No, they are executed in a procedural fashion.


Old answer:

1) Would it be likely for one of these enterframes to be less cpu intensive than the other?

Well if you look at the amount of instructions the one dispatching a event have more instructions until it actually executes the code in the methods. Although the event dispatching creates a new thread for each method so they run simultaneously. The time you spend for those extra instructions to call the method you will gain in the thread splitting. So if you have a lot of instructions in each method I would say the event dispatching is faster.

2) In the first example does the frame have to wait until method 1 2 and 3 have been completed before the next frame can render and execute code?

Yes.

3) In the 2nd example could the events dispatched to method 1 2 and 3 be executed in a frame other than the one that called it? EG, if the the cpu is under pressure can it defer the execution?

Since the code is executed in a separate thread and the enter frame continues the methods can be executed multiple times before the first one finished depending on how much instructions there are in each method.


It looks, you're mistaken in dispatchEvent(MethodEvent.Test, method1); You should use addEventListener function first.

addEventListener(MethodEvent.Test, method1);
addEventListener(MethodEvent.Test, method2);
addEventListener(MethodEvent.Test, method3);

And then dispatchEvent(MethodEvent.Test);. There's no significant difference in performance between these 2 variants. Dispatching event is just calling each function in array of listeners. So the only performance issue is in looping through listeners array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜