简体   繁体   English

C&C ++编译器是否优化了与函数调用的比较?

[英]Do C & C++ compilers optimize comparisons with function calls?

Do C and C++ compilers generally optimize comparisons with functions? 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). 例如, 这个页面表明,在一些标准库实现中,C ++中std :: lists上的size函数可能具有线性复杂度O(N)(这对于链表是有意义的)。

But in that case, if myList is a huge list, what would something like this do? 但在这种情况下,如果myList是一个巨大的列表,这样的事情会怎样呢?

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

Would the size() function find and count all N list members, or would it be optimized to short circuit after finding 5 members? size()函数会查找并计算所有N个列表成员,还是会在找到5个成员后优化为短路?

Theoretically the possibility exists if size() was inlined, but to perform the optimization the compiler would have to 理论上,如果size()被内联,则存在这种可能性,但是要执行编译器必须进行的优化

  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. 所以不,通常你不应该期望这样的情况得到优化。

Actually, in C++11, std::list is optimized and size() is returned in constant time. 实际上,在C ++ 11中, std::list已经过优化,并且size()在常量时间内返回。

For C++03, size() indeed operates in linear time, as it needs to count the elements each time. 对于C ++ 03, size()确实在线性时间内运行,因为它每次都需要计算元素。

Would the size() function find and count all N list members, or would it be optimized to short circuit after finding 5 members? size()函数会查找并计算所有N个列表成员,还是会在找到5个成员后优化为短路?

Never seen this sort of optimization happening in practice. 从未见过这种优化在实践中发生。 While it's certainly legal, I doubt there's any compiler that actually implements something like this. 虽然它肯定是合法的,但我怀疑是否有任何编译器实际上实现了这样的东西。

The myList.size() function itself has no way to be compiled for the purpose you're using it for, so it will determine the entire size. myList.size()函数本身无法为您正在使用它的目的进行编译,因此它将确定整个大小。 In order to get the optimization you're suggesting, you would need a dedicated predicate function instead of the general size() , something like bool sizeLessThan(int); 为了获得你所建议的优化,你需要一个专用的谓词函数而不是一般的size() ,比如bool sizeLessThan(int); .

NO You are asking if the compiler can make a function behave differently depending on how its results are used. 您是在询问编译器是否可以使函数的行为有所不同,具体取决于其结果的使用方式。 This could only potentially be done for inline functions where the caller and the function will be compiled together at the same time. 这只能用于内联函数,其中调用者和函数将同时编译在一起。 It seems quite a stretch for anything beyond that. 对于任何超出此范围的事情,这似乎相当长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM