dan*_*njp 3 flash actionscript actionscript-3
只是想知道使用是否有任何优势
private static const
代替
private const
私人常数?如果您只有一个类或多个实例,这会改变吗?我怀疑如果你有多个类的实例,使用中可能会有一些小的内存/性能优势static.
正如mmsmatt指出的那样,它们可以节省一些内存.通常这不是节省内存的最佳位置.您应该更担心内存泄漏,一般关于有效的文件格式和数据表示.
静态常量的缺点是所有全局访问都比本地访问慢.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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
private static const 每种类型存储一次成员.
private const 每个实例存储一次成员.
所以是的,你节省了一些记忆.