现代编译器会自动优化以下 C++代码?

发布于 2025-01-06 21:03:28 字数 349 浏览 1 评论 0原文

如果我使用以下代码,编译器是否会像使用二叉树搜索值的开关结构一样优化它?

   if ( X == "aaaa" || X == "bbbb" || X == "cccc" || X == "dddd" )
   {

   }
   else if ( X == "abcd" || X == "cdef" || X == "qqqq" )
   {

   }

这只是一个例子,引号内的内容没有任何模式

UPDATE

好吧,X 是一个字符串,但我真的认为这并不重要,我只是想知道,当如果都是关于单个变量的,它会被优化吗?

If i use hte following codes , will the compiler optimize it like a switch structure , which uses a binary tree to search for values ?

   if ( X == "aaaa" || X == "bbbb" || X == "cccc" || X == "dddd" )
   {

   }
   else if ( X == "abcd" || X == "cdef" || X == "qqqq" )
   {

   }

It's just an example , there's no pattern of what's inside the quote symbol

UPDATE

Ok , X is a string , but i don't really think it matters here , i just want to know , when everything inside the if was all about single variable , will it be optimized.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愿得七秒忆 2025-01-13 21:03:28

这些值将被一个接一个地比较,因为这是 || 或短路运算符的要求。因此,这里会发生两件事:

  • X 将从右到左进行一一比较。
  • 任何比较成功后都不会再进行比较(因为它是短路 OR 运算符),即在以下情况下

例如:

int hello() {
    std::cout<<"Hello";
    return 10;
}

int world() {
    std::cout<<"World";
    return 11;
}

int hello2() {
    std::cout<<"Hello2";
    return 9;
}

int a = 10;

bool dec = (a == hello() || a == world())
bool dec = (a == hello2() || a == hello() || a == world())

第一个语句的输出将是:

Hello

as a == world()< /code> 将不会被执行,对于第二个 Hello2 Hello,因为比较会继续发生,直到第一次成功。

对于 && 运算符,比较会一直进行,直到第一次失败(因为这足以确定整个语句的结果)。

The values WILL be compared one after the other as it is a requirement of the || or, the short-circuit operator. So, here two things will happen:

  • X will be compared one-by-one from right-to-left.
  • There will be NO MORE comparisons after any comparison that succeeds (Since it is the short-circuit OR operator) i.e. in the following case

For example:

int hello() {
    std::cout<<"Hello";
    return 10;
}

int world() {
    std::cout<<"World";
    return 11;
}

int hello2() {
    std::cout<<"Hello2";
    return 9;
}

int a = 10;

bool dec = (a == hello() || a == world())
bool dec = (a == hello2() || a == hello() || a == world())

The output for the first statement will be:

Hello

as a == world() will not be executed, and for the second Hello2 Hello, as the comparisons keep on happening till the first success.

In case of the && operator, the comparisons keep on happening until the first failure (as that is enough to determine the outcome of the entire statement).

会发光的星星闪亮亮i 2025-01-13 21:03:28

几乎可以肯定不是。二分查找需要某种排序
关系,以及比较小于的能力。编译器
不能假设这样的存在,即使它确实找到了一个,它也不能
假设它定义了一个等价关系,对应于
<代码>==。编译器也可能无法确定
定义排序关系的函数没有副作用。 (如果它
有副作用,或者表达式中的任何操作有副作用
影响,编译器必须尊重短路行为
||.) 最后,即使编译器做了这一切...如果我
仔细选择比较的顺序,以便最频繁的比较
案例是第一个。这样的“优化”甚至可能最终
是一种悲观主义。

处理这个问题的“正确”方法是创建一个地图,将
字符串到函数指针(或多态函数对象,
如果涉及某个州)。

Almost certainly not. Binary search requires some sort of ordering
relationship, and an ability to compare for less-than. The compiler
cannot assume such exists, and even if it does find one, it cannot
assume that it defines an equivalence relation which corresponds to
==. It's also possible that the compiler can't determine that the
function defining the ordering relationship has no side effects. (If it
has side effects, or if any operation in the expression has side
effects, the compiler must respect the short circuiting behavior of
||.) Finally, even if the compiler did all this... what happens if I
carefully chose the order of the comparisons so that the most frequent
case is the first. Such an “optimization” could even end up
being a pessimization.

The “correct” to handle this is to create a map, mapping the
strings to pointers to functions (or to polymorphic functional objects,
if some state is involved).

凑诗 2025-01-13 21:03:28

这可能取决于您设置的标志。二叉树的搜索速度更快,但通常需要处理更多代码。因此,如果您针对大小进行了优化,则可能不会。我不确定它是否会这样做。你知道,gcc 根据很多标志进行了优化。 O1、O2、O3、O4 只是表示大组标志的简单形式。您可以在此处找到所有优化标志的列表: http://gcc.gnu .org/onlinedocs/gcc/Optimize-Options.html

尝试在该页面中搜索字符串、二叉树等。

It probably depends on the flags you set. Binary trees are faster for search but usually require more code to be handled. So if you optimized for size it probably wont. I'm not sure if it will do it anyway. You know, gcc is optimized according to a LOT of flags. O1, O2, O3, O4 are just simple forms to indicate big groups of flags. You can find a list of all the optimization flags here: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Try to search for strings, binary trees, etc in that page.

波浪屿的海角声 2025-01-13 21:03:28

测试这一点的最佳方法是下面的示例。

int a = 0;
int b = 0;
if(a == a || b++) {
    ...
}
cout << b;

b 变量的值末尾应为 0b++ 部分不会被执行。这是预期的行为,但可能存在一些例外情况,具体取决于编译器及其优化设置。甚至许多脚本语言(例如 JavaScript)也有这种行为方式。

Best way to test this is the example like below.

int a = 0;
int b = 0;
if(a == a || b++) {
    ...
}
cout << b;

value of the b variable should be 0 at the end. b++ part won't be executed. that's the expected behaviour but there might be some exceptions depending on the compiler and its optimization settings. even many scripting langauges like JavaScript behaves this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文