虽然有关代码提示和错误检查其他答案点是准确的,我想,以解决有关性能的要求。事实并非如此。理论上,强类型允许编译器生成更接近本机的代码。尽管目前的VM,这种优化不会发生。在这里和那里,AS3编译器将采用整数指令而不是浮点指令。否则,类型指示器在运行时不会产生太大影响。
例如,请考虑下面的代码:
function hello():String {
return "Hello";
}
var s:String = hello() + ' world';
trace(s);
我这里还有的AVM2操作码从它产生的:
getlocal_0
pushscope
getlocal_0
getlocal_0
callproperty 4 0 ; call hello()
pushstring 12 ; push ' world' onto stack
add ; concatenate the two
initproperty 5 ; save it to var s
findpropstrict 7 ; look up trace
getlocal_0 ; push this onto stack
getproperty 5 ; look up var s
callpropvoid 7 1 ; call trace
returnvoid
现在,如果我去掉类型的指标,我得到以下:
getlocal_0
pushscope
getlocal_0
getlocal_0
callproperty 3 0
pushstring 11
add
initproperty 4
findpropstrict 6
getlocal_0
getproperty 4
callpropvoid 6 1
returnvoid
完全一样,除了所有的名字索引都少一个'String'no更长的时间出现在常量表中。
我不想阻止人们使用强打字。一个人不应该期待性能方面的奇迹。
编辑:如果有人有兴趣,我已经把我的AS3字节码反汇编在线:
http://flaczki.net46.net/codedump/
,以便它现在取消引用操作数我已经改善它。
这是一个很不好的例子。它比较动态对象与封闭对象的初始化。性能差异与强/弱打字无关。 – cleong 2012-08-14 23:08:24