测试了以下内容:
var timer:int = getTimer();
trace(timer);
for ( var g:int = 0; g < 10000000; ++g)
{
var mod0:int = g % 10;
var sum0:String = "";
switch(mod0)
{
case 0: sum0 = "0"; break;
case 1: sum0 = "1"; break;
case 2: sum0 = "2"; break;
case 3: sum0 = "3"; break;
case 4: sum0 = "4"; break;
case 5: sum0 = "5"; break;
case 6: sum0 = "6"; break;
case 7: sum0 = "7"; break;
case 8: sum0 = "8"; break;
case 9: sum0 = "9"; break;
default: break;
}
}
timer = getTimer();
trace(timer);
for ( var h:int = 0; h < 10000000; ++h)
{
var mod1:int = h % 10;
var sum1:String = "";
if (mod1 == 0) sum1 = "0";
else if (mod1 == 1) sum1 = "1";
else if (mod1 == 2) sum1 = "2";
else if (mod1 == 3) sum1 = "3";
else if (mod1 == 4) sum1 = "4";
else if (mod1 == 5) sum1 = "5";
else if (mod1 == 6) sum1 = "6";
else if (mod1 == 7) sum1 = "7";
else if (mod1 == 8) sum1 = "8";
else if (mod1 == 9) sum1 = "9";
}
timer = getTimer();
trace(timer);
计时器给出:
221
443
636
,这意味着switch
需要222毫秒而if-else
需要193毫秒,为什么?
对于我在C ++中所知道的,switch
更快,因为它编译成跳转表汇编代码,在AS3中不应该相同吗?
答案 0 :(得分:5)
这里有一个非常全面的看法:http://jacksondunstan.com/articles/793
TL; DR:动作脚本编译器有点糟糕。在AS3中,switch语句比例如C / C ++要复杂得多,因为它可以在非整数类型上工作。