gpt4 book ai didi

performance - if-else 比 switch 快,为什么? (在 ActionScript3 中)

转载 作者:行者123 更新时间:2023-12-02 22:15:17 27 4
gpt4 key购买 nike

测试了以下内容:

        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);

计时器给出:221443636,这意味着 switch 需要 222ms 而 if-else 需要 193ms,为什么?
据我所知,在 C++ 中,switch 更快,因为它编译为跳转表汇编代码,在 AS3 中不应该一样吗?

最佳答案

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

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

关于performance - if-else 比 switch 快,为什么? (在 ActionScript3 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com