English 中文(简体)
C & C++汇编者是否优化了与功能要求的比较?
原标题:Do C & C++ compilers optimize comparisons with function calls?

C和C++汇编者是否普遍优化与功能的比较?

For example, this page suggests that the size function on std::lists in C++ can have a linear complexity O(N) in some standard library implementations (which makes sense for a linked list).

但在这种情况下,如果<代码>myList是一个巨大的清单,那么这样做会是什么?

    if (myList.size() < 5) return 1;
    else return 2;

大小功能是否发现和计算所有N类名单成员,或者在找到5名成员后将优化到短路上?

最佳回答

理论上说,如果列入<条码>(<>>>>>>,则有可能实现最佳化,则汇编者必须加以优化。

  1. Detect that you are testing specifically a "less than" condition
  2. Prove that the loop (assume one exists for the purposes of this discussion) results in a variable increasing monotonically
  3. Prove that there are no observable side effects from the loop body

That s a big bunch of things to count on IMHO, and it includes features which are not "obviously useful" in other contexts as well. Keep in mind that compiler vendors have limited resources so there has to be really good justification for implementing these prerequisites and having the compiler bring all the parts together to optimize this case.

Seeing as even if this is a perf issue for someone the problem can be easily solved in code, I don t feel that there would be such justification. So no, generally you should not expect cases like this to be optimized.

问题回答

实际上,在C++11,std:list 优化,size(<>/code> 定期返还。

关于C++03,size(>) 确实是在线性时间运行的,因为它需要每次计算内容。

大小功能是否发现和计算所有N类名单成员,或者在找到5名成员后将优化到短路上?

在实践中从未看到这种优化。 虽然这肯定是法律的,但我怀疑是否有任何编纂者实际执行类似的东西。

我的神职人员本身无法为你重新使用它的目的进行编辑,因此它将决定整个规模。 为了优化你的建议,你需要一种专用的上游功能,而不是一般的<代码>size(,例如bool smallLessThan(int);

<><0>> 您询问,汇编者能否根据成果的利用情况,以不同的方式行事。 只有在电话和功能同时汇编的网上功能方面,才能做到这一点。 这似乎远远超出了这一点。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签