gpt4 book ai didi

c++ - 使用 goto 进行优化

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:25:09 25 4
gpt4 key购买 nike

您好,如果使用 goto 是优化的好习惯,我正在徘徊。我知道这是一件野蛮的事情,但是,说真的。

例如这样写:

switch(command[0].cmd)
{
case 0: // turn off
s::s_off();
S_time = command[0].length;
break;
case 1: // turn on
s::s_on();
S_time = command[0].length;
break;
case 4: // nop

break;
}

像这样:

switch(command[0].cmd)
{
case 0: // turn off
s::s_off();
goto a;
break;
case 1: // turn on
s::s_on();
goto a;
break;
case 4: // nop
goto b;
break;
}

a:
S_time = command[0].length;
b:

最佳答案

确实,如果可能的话,避免 goto 是明智的,并相信编译器会为您进行优化。即使在您的情况下,也有另一种方法可以避免代码重复:

/*possibly inline*/ void foo(/*pass necessary parameters*/)
{
switch(command[0].cmd){
case 0: // turn off
s::s_off();
break;
case 1: // turn on
s::s_on();
break;
case 4: // nop
return;
}
S_time = command[0].length;
}

关于c++ - 使用 goto 进行优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41608897/

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