现代编译器会自动优化以下 C++代码?
如果我使用以下代码,编译器是否会像使用二叉树搜索值的开关结构一样优化它?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些值将被一个接一个地比较,因为这是
||
或短路运算符的要求。因此,这里会发生两件事:例如:
第一个语句的输出将是:
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:For example:
The output for the first statement will be:
as
a == world()
will not be executed, and for the secondHello2 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).几乎可以肯定不是。二分查找需要某种排序
关系,以及比较小于的能力。编译器
不能假设这样的存在,即使它确实找到了一个,它也不能
假设它定义了一个等价关系,对应于
<代码>==。编译器也可能无法确定
定义排序关系的函数没有副作用。 (如果它
有副作用,或者表达式中的任何操作有副作用
影响,编译器必须尊重短路行为
||
.) 最后,即使编译器做了这一切...如果我仔细选择比较的顺序,以便最频繁的比较
案例是第一个。这样的“优化”甚至可能最终
是一种悲观主义。
处理这个问题的“正确”方法是创建一个地图,将
字符串到函数指针(或多态函数对象,
如果涉及某个州)。
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 thefunction 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 Icarefully 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).
这可能取决于您设置的标志。二叉树的搜索速度更快,但通常需要处理更多代码。因此,如果您针对大小进行了优化,则可能不会。我不确定它是否会这样做。你知道,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.
测试这一点的最佳方法是下面的示例。
b
变量的值末尾应为0
。b++
部分不会被执行。这是预期的行为,但可能存在一些例外情况,具体取决于编译器及其优化设置。甚至许多脚本语言(例如JavaScript
)也有这种行为方式。Best way to test this is the example like below.
value of the
b
variable should be0
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 likeJavaScript
behaves this way.