<legend id='ZMyq4'><style id='ZMyq4'><dir id='ZMyq4'><q id='ZMyq4'></q></dir></style></legend>
      <bdo id='ZMyq4'></bdo><ul id='ZMyq4'></ul>

  1. <tfoot id='ZMyq4'></tfoot>
    1. <i id='ZMyq4'><tr id='ZMyq4'><dt id='ZMyq4'><q id='ZMyq4'><span id='ZMyq4'><b id='ZMyq4'><form id='ZMyq4'><ins id='ZMyq4'></ins><ul id='ZMyq4'></ul><sub id='ZMyq4'></sub></form><legend id='ZMyq4'></legend><bdo id='ZMyq4'><pre id='ZMyq4'><center id='ZMyq4'></center></pre></bdo></b><th id='ZMyq4'></th></span></q></dt></tr></i><div id='ZMyq4'><tfoot id='ZMyq4'></tfoot><dl id='ZMyq4'><fieldset id='ZMyq4'></fieldset></dl></div>

      <small id='ZMyq4'></small><noframes id='ZMyq4'>

      哪些(如果有)C++ 编译器进行尾递归优化?

      时间:2023-09-18
        <legend id='qHSZE'><style id='qHSZE'><dir id='qHSZE'><q id='qHSZE'></q></dir></style></legend>

            <bdo id='qHSZE'></bdo><ul id='qHSZE'></ul>
            <tfoot id='qHSZE'></tfoot>
                <tbody id='qHSZE'></tbody>

              <small id='qHSZE'></small><noframes id='qHSZE'>

                <i id='qHSZE'><tr id='qHSZE'><dt id='qHSZE'><q id='qHSZE'><span id='qHSZE'><b id='qHSZE'><form id='qHSZE'><ins id='qHSZE'></ins><ul id='qHSZE'></ul><sub id='qHSZE'></sub></form><legend id='qHSZE'></legend><bdo id='qHSZE'><pre id='qHSZE'><center id='qHSZE'></center></pre></bdo></b><th id='qHSZE'></th></span></q></dt></tr></i><div id='qHSZE'><tfoot id='qHSZE'></tfoot><dl id='qHSZE'><fieldset id='qHSZE'></fieldset></dl></div>

                本文介绍了哪些(如果有)C++ 编译器进行尾递归优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在我看来,在 C 和 C++ 中进行尾递归优化会非常有效,但是在调试时我似乎从未看到指示这种优化的帧堆栈.这很好,因为堆栈告诉我递归有多深.但是,优化也会很好.

                It seems to me that it would work perfectly well to do tail-recursion optimization in both C and C++, yet while debugging I never seem to see a frame stack that indicates this optimization. That is kind of good, because the stack tells me how deep the recursion is. However, the optimization would be kind of nice as well.

                是否有任何 C++ 编译器进行此优化?为什么?为什么不呢?

                Do any C++ compilers do this optimization? Why? Why not?

                我该如何告诉编译器这样做?

                How do I go about telling the compiler to do it?

                • 对于 MSVC:/O2/Ox
                • 对于 GCC:-O2-O3
                • For MSVC: /O2 or /Ox
                • For GCC: -O2 or -O3

                在某种情况下如何检查编译器是否这样做了?

                How about checking if the compiler has done this in a certain case?

                • 对于 MSVC,启用 PDB 输出以便能够跟踪代码,然后检查代码
                • 对于海湾合作委员会......?

                我仍然会就如何确定某个函数是否被编译器这样优化提出建议(尽管我发现 Konrad 告诉我假设它让我放心)

                I'd still take suggestions for how to determine if a certain function is optimized like this by the compiler (even though I find it reassuring that Konrad tells me to assume it)

                始终可以通过进行无限递归并检查它是否导致无限循环或堆栈溢出来检查编译器是否完全执行此操作(我使用 GCC 执行此操作并发现 -O2 就足够了),但我希望能够检查某个我知道无论如何都会终止的函数.我很想有一个简单的方法来检查这个:)

                It is always possible to check if the compiler does this at all by making an infinite recursion and checking if it results in an infinite loop or a stack overflow (I did this with GCC and found out that -O2 is sufficient), but I want to be able to check a certain function that I know will terminate anyway. I'd love to have an easy way of checking this :)

                经过一些测试,我发现析构函数破坏了进行这种优化的可能性.有时值得更改某些变量和临时变量的范围,以确保它们在 return 语句开始之前超出范围.

                After some testing, I discovered that destructors ruin the possibility of making this optimization. It can sometimes be worth it to change the scoping of certain variables and temporaries to make sure they go out of scope before the return-statement starts.

                如果在尾调用之后需要运行任何析构函数,则无法进行尾调用优化.

                If any destructor needs to be run after the tail-call, the tail-call optimization can not be done.

                推荐答案

                目前所有主流编译器都执行尾调用优化相当不错(并且已经这样做了十多年),甚至对于相互递归调用,例如:

                All current mainstream compilers perform tail call optimisation fairly well (and have done for more than a decade), even for mutually recursive calls such as:

                int bar(int, int);
                
                int foo(int n, int acc) {
                    return (n == 0) ? acc : bar(n - 1, acc + 2);
                }
                
                int bar(int n, int acc) {
                    return (n == 0) ? acc : foo(n - 1, acc + 1);
                }
                

                让编译器进行优化很简单:只需打开优化以提高速度:

                Letting the compiler do the optimisation is straightforward: Just switch on optimisation for speed:

                • 对于 MSVC,使用 /O2/Ox.
                • 对于 GCC、Clang 和 ICC,使用 -O3

                检查编译器是否进行了优化的一种简单方法是执行一个否则会导致堆栈溢出的调用——或者查看程序集输出.

                An easy way to check if the compiler did the optimisation is to perform a call that would otherwise result in a stack overflow — or looking at the assembly output.

                作为一个有趣的历史记录,在 毕业论文,作者:Mark Probst.该论文描述了实施中的一些有趣的注意事项.值得一读.

                As an interesting historical note, tail call optimisation for C was added to the GCC in the course of a diploma thesis by Mark Probst. The thesis describes some interesting caveats in the implementation. It's worth reading.

                这篇关于哪些(如果有)C++ 编译器进行尾递归优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:std::map 键的部分匹配 下一篇:是“for(;;)"比“while(真)"快?如果不是,人

                相关文章

                最新文章

                1. <legend id='7xcnr'><style id='7xcnr'><dir id='7xcnr'><q id='7xcnr'></q></dir></style></legend>

                  <i id='7xcnr'><tr id='7xcnr'><dt id='7xcnr'><q id='7xcnr'><span id='7xcnr'><b id='7xcnr'><form id='7xcnr'><ins id='7xcnr'></ins><ul id='7xcnr'></ul><sub id='7xcnr'></sub></form><legend id='7xcnr'></legend><bdo id='7xcnr'><pre id='7xcnr'><center id='7xcnr'></center></pre></bdo></b><th id='7xcnr'></th></span></q></dt></tr></i><div id='7xcnr'><tfoot id='7xcnr'></tfoot><dl id='7xcnr'><fieldset id='7xcnr'></fieldset></dl></div>
                  1. <small id='7xcnr'></small><noframes id='7xcnr'>

                      <bdo id='7xcnr'></bdo><ul id='7xcnr'></ul>
                  2. <tfoot id='7xcnr'></tfoot>