actionscript-3 - "calling methods directly instead of through a reference"是什么意思?

标签 actionscript-3

我刚刚经历了a slideshow from Grant Skinner关于提高 ActionScript 的性能。他给出的一个提示是“直接调用方法而不是通过引用”。我不太明白这是什么意思...

是否意味着避免对引用函数对象的变量进行函数调用?

var someObjectsDoSomethingMethod:Function = someObj.doSomething;
someObjectsDoSomethingMethod();

还是避免对引用另一个拥有该方法的对象的变量进行函数调用?
var someObject:Function = someObj;
someObject.doSomething();

或者 ...?

最佳答案

你走在正确的轨道上,他说,如果可能的话(特别是对于循环内的方法调用),最快的方法是根本不使用方法调用(参见幻灯片 51),但如果你必须尝试直接调用一种方法(幻灯片 52)。虽然这并不总是实际的或理想的,但根据调用方法的方式以及调用它的对象,会产生一些开销。

最大的收获是关闭非常昂贵。

例如:

WithAnonymousFunction.Go(function() { /* Do Work */ });  // Slow
WithReference.Go(new WorkerClass()); // Faster
WithAnonymousReference.Go((new WorkerClass()).doWork); // Faster
WithMethod.Go(); // Even faster!
WithInlinedMthod.Go() // Fastest!

public class WithAnonymousFunction {
    public static function Go(func:Function):void { 
        func();         
    }
}

public class WorkerClass {
    public doWork() { // Do Work }
}

public class WithAnonymousReference {        
    public static function Go(func:Function) {
        func();         
    }
}

public class WithReference {
    public static function Go(cls:WorkerClass) {
        cls.doWork()
    }
}   

public class WithMethod {
    public static function Go() {
        doWork();
    }

    public static function doWork():void { // Do Work }
}

public class WithInlinedMethod {
    public static function Go() {
        // Do Work
    }
}

我不确定的一件事是使用具体类与接口(interface)之间的性能差异。

关于actionscript-3 - "calling methods directly instead of through a reference"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111154/

相关文章:

actionscript-3 - AS3 将一种类型转换为另一种类型

java - 从 Flex 调用 jar 文件命令

actionscript-3 - 有没有办法运行两个调试器实例?

performance - iPad 上的 Adob​​e AIR 滚动性能 (2) - 我能期待什么?

actionscript-3 - 关于带接口(interface)的类实现问题

android - 在 Air Mobile 中加载外部交互式 swf

c# - 翻译我写到 C# 的 ActionScript 3 代码

actionscript-3 - globals 或 'evil' singletons 有什么意义吗

c++ - 使用数组类型初始化 AS 3.0 vector - Vector.<Array>? (加上 C++ 等价物)

apache-flex - 通过 URLRequest 上传 Youtube?