测试了以下内容:
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 中不应该相同吗?