2

测试了以下内容:

        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-else193 毫秒,为什么?
对于我在 C++ 中所知道的,switch它更快,因为它编译为跳转表汇编代码,在 AS3 中不应该相同吗?

4

1 回答 1

5

这里有一个非常彻底的了解:http: //jacksondunstan.com/articles/793

TL;DR:actionscript 编译器有点糟糕。此外,AS3 中的 switch 语句比例如 C/C++ 复杂得多,因为它可以在非整数类型上工作。

于 2013-01-29T10:53:31.717 回答