Flash 使用'的任何优势;静态';关于私人警察?

Flash 使用'的任何优势;静态';关于私人警察?,flash,actionscript-3,actionscript,Flash,Actionscript 3,Actionscript,只是想知道使用 private static const 而不是 private const 对于私有常量? 如果只有一个或多个类实例,这种情况会改变吗? 如果类有多个实例,我怀疑使用static可能会有一些小内存/性能优势。私有static const每个类型存储一次成员 private const每个实例存储一次成员 因此,是的,您正在节省一些内存。正如mmsmart所指出的,它们可以节省一些内存。然而,通常这不是保存内存的最佳地方。您应该更担心内存泄漏、高效的文件格式和数据表示 静态常量的

只是想知道使用


private static const

而不是

private const

对于私有常量? 如果只有一个或多个类实例,这种情况会改变吗?
如果类有多个实例,我怀疑使用
static
可能会有一些小内存/性能优势。

私有static const
每个类型存储一次成员

private const
每个实例存储一次成员


因此,是的,您正在节省一些内存。

正如mmsmart所指出的,它们可以节省一些内存。然而,通常这不是保存内存的最佳地方。您应该更担心内存泄漏、高效的文件格式和数据表示

静态常量的一个缺点是,所有全局访问都比本地访问慢
instance.ident
优于
Class.ident
。运行此代码以测试:

package  {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.*;
    public class Benchmark extends Sprite {
        private static const delta:int = 0;
        private const delta:int = 0;        
        private var output:TextField;
        public function Benchmark() {
            setTimeout(doBenchmark, 1000);
            this.makeOutput();
        }
        private function doBenchmark():void {
            var i:int, start:int, sum:int, inst:int, cls:int;

            start = getTimer();
            sum = 0;
            for (i = 0; i < 100000000; i++) sum += this.delta;
            out("instance:", inst = getTimer() - start);

            start = getTimer();
            sum = 0;
            for (i = 0; i < 100000000; i++) sum += Benchmark.delta;
            out("class:", cls = getTimer() - start);

            out("instance is", cls/inst, "times faster");
        }   
        private function out(...args):void {
            this.output.appendText(args.join(" ") + "\n");
        }
        private function makeOutput():void {
            this.addChild(this.output = new TextField());
            this.output.width = stage.stageWidth;
            this.output.height = stage.stageHeight;
            this.output.multiline = this.output.wordWrap = true;
            this.output.background = true;          
        }       
    }
}
包{
导入flash.display.Sprite;
导入flash.text.TextField;
导入flash.utils。*;
公共类基准扩展了Sprite{
私有静态常量增量:int=0;
私有常量增量:int=0;
私有变量输出:TextField;
公共职能基准(){
设置超时(doBenchmark,1000);
这是makeOutput();
}
私有函数doBenchmark():void{
变量i:int,起始:int,和:int,指令:int,cls:int;
start=getTimer();
总和=0;
对于(i=0;i<100000000;i++)和+=this.delta;
out(“实例:”,inst=getTimer()-start);
start=getTimer();
总和=0;
对于(i=0;i<100000000;i++)和+=Benchmark.delta;
out(“类:”,cls=getTimer()-start);
out(“实例是”,cls/inst,“快几倍”);
}   
专用函数输出(…参数):无效{
this.output.appendText(args.join(“”+“\n”);
}
私有函数makeOutput():void{
this.addChild(this.output=new TextField());
this.output.width=stage.stageWidth;
this.output.height=stage.stageHeight;
this.output.multiline=this.output.wordWrap=true;
this.output.background=true;
}       
}
}

这取决于具体情况

如上所述,静态将是每个类型一次,非静态将是每个实例一次,因此这取决于您将拥有多少实例

我这么说是因为如果你有一个一次只实例化一次的组件(比如一个弹出的提示),然后你从内存中完全处理掉它,那么这意味着你使用的是不必要的静态内存,因为它永远不会离开这个静态变量。
如果您有多个实例(如粒子或多个窗口),则最好使用静态,因为它们将共享同一个变量。

如果该类不一定需要实例化,该怎么办?在这种情况下,使用非静态方法可能会节省内存。对于好奇和懒惰的人,我在Flash Player调试器版本11.7.700.169(11.6)的调试模式下运行了几次这段代码,我得到的结果是,
实例的平均速度是
的1.01倍。但是,当我将
getTimer()-start
调用移到
out(…)
调用之外时,我平均每个测试的
实例快了0.84倍。所以看起来,与三年前back2dos断言的相反,现在这个测试用例中的全局访问实际上比本地访问快。@mziwisky:首先,不要在debug player中进行基准测试。这完全歪曲了结果。第二,支持我最初的主张。