Flex 性能、方法调用或事件
我正在尝试减少我的应用程序的一些 cpu 周期,并且我想知道执行代码时占用 cpu 最少的方法是什么。
以这 2 个例子为例。第一个是运行并调用各种方法的输入框架。 第二个是相同的输入框架,但改为分派事件。
1) 这些 Enterframe 中的一个是否可能比另一个的 CPU 密集度更低?
2) 在第一个示例中,帧是否必须等到方法 1、2 和 3 完成后,下一帧才能渲染并执行代码?
3) 在第二个示例中,分派给方法 1、2 和 3 的事件是否可以在调用它的框架之外的框架中执行? EG,如果cpu有压力可以推迟执行吗?
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为优化方面你能做的最好的事情就是忘记
enterFrame
事件。在代码中放置一个Timer
,并根据您的具体需要设置延迟。您可以轻松体验巨大的优化。据我所知
enterFrame
连接到 fps。因此,如果您的电影是 30 fps 动画,那么它将每秒运行 30 次。现在,这意味着它更像是一个间隔 (
flash.utils.setInterval
) 或Timer
。因此您可以看到,enterFrame 或多或少是一种设置间隔或类似间隔的功能的简单方法。如果您了解自己的需求、刷新率等,您可以更精确地设置计时器和间隔,以获得巨大的性能提升。
I think the best you can do about optimization is forget
enterFrame
event. Place aTimer
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 aTimer
.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.
这是过早优化的典型案例。无论您使用事件驱动方法还是方法调用都应该基于良好的设计实践,而不是基于哪一种更快。
无论如何,Flex 大多是异步的,它使用事件驱动模型来维护松散的组件耦合和事件链,并且在后期概率中您正在寻找的方法将无助于实质性的性能改进(尤其是 CPU 周期??)。如果您认为性能不是最佳,我建议您使用 Flex Profiler。 (主要是它在内存中存储组件,这会减慢 Flex 应用程序的速度。只要您释放过时的组件引用...)
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...)
编辑:由于我对 Flash 播放器中的内置功能分派的事件和 as3 代码分派的自定义事件感到困惑,所以我的第一个答案是不正确的。尽管Flash播放器本身是多线程的,但您无法访问as3中的线程功能。这是我修改后的答案:
好吧,如果您查看指令数量,就会发现调度事件的指令有更多指令,直到它实际执行方法中的代码。它首先必须遍历事件侦听器列表,并与给定的事件类型进行比较,在该特定对象中查找正确的事件侦听器。
是的。同样的情况也发生在第二个例子中。
不,它们是按程序执行的。
旧答案:
好吧,如果您查看指令数量,就会发现调度事件的指令有更多指令,直到它实际执行方法中的代码。尽管事件调度为每个方法创建了一个新线程,因此它们同时运行。您花费在这些额外指令上的时间来调用您将在线程拆分中获得的方法。因此,如果每个方法都有很多指令,我会说事件分派更快。
是的。
由于代码是在单独的线程中执行的,并且输入帧继续,因此在第一个方法完成之前可以多次执行方法,具体取决于每个方法中有多少指令。
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:
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.
Yes. Same thing happens in the second example.
No, they are executed in a procedural fashion.
Old answer:
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.
Yes.
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.
看起来,您在
dispatchEvent(MethodEvent.Test, method1);
中出错了,您应该使用 addEventListener 函数首先。然后
dispatchEvent(MethodEvent.Test);
。这两种变体之间的性能没有显着差异。调度事件只是调用侦听器数组中的每个函数。因此,唯一的性能问题是循环侦听器数组。It looks, you're mistaken in
dispatchEvent(MethodEvent.Test, method1);
You should use addEventListener function first.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.