哪个,如果有的话,C ++编译器做尾递归优化? [英] Which, if any, C++ compilers do tail-recursion optimization?

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

问题描述

在我看来,它可以很好地在C和C ++中进行尾递归优化,而在调试时,我似乎从来没有看到一个帧堆栈指示这种优化。这是一种好,因为堆栈告诉我递归是多深。但是,优化也会很好。



任何C ++编译器做这个优化?为什么?为什么不呢?



如何告诉编译器这样做?




  • 对于MSVC:/ O2或/ Ox

  • 对于GCC:-O2或-O3



<




  • 对于MSVC,使能PDB输出能够跟踪代码,然后检查代码

  • 对于GCC ..?



建议如何确定某个函数是否由编译器优化(尽管我发现它令人放心Konrad告诉我假设它)



这是始终可能的通过进行无限递归检查编译器是否做到这一点,并检查是否导致无限循环或堆栈溢出(我用GCC做到这一点,发现-O2是足够的),但我想要能够检查我知道将会终止的某个功能。我想有一个简单的方法来检查这个:)






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



如果任何析构函数需要

解决方案

当前版本的VC ++和GCC做相当好的尾调用优化,甚至相互递归调用。我敢打赌英特尔编译器也是。



让编译器做优化是很简单的:只是打开优化的速度。但是你这样做,对吧? ; - )




  • 对于MSVC,使用/ O2或/ Ox。

  • -O3



检查编译器是否进行优化(我知道)的最简单方法是执行一个调用在堆栈溢出或查看组件输出。但是,你通常只是假设编译器做了优化。



/编辑:C的尾调用优化已经添加到GCC在 Mark Probst的文凭论文。论文描述了一些有趣的注意事项的执行。这是值得阅读。


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.

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

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

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

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

  • For MSVC, enable PDB output to be able to trace the code, then inspect the code
  • For GCC..?

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)

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 :)


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.

解决方案

Both the current version of VC++ and GCC do tail call optimizations fairly well and even for mutually recursive calls. I bet the Intel compiler does, too.

Letting the compiler do the optimization is straightforward: Just switch on optimization for speed. But you do that anyway, right? ;-)

  • For MSVC, use /O2 or /Ox.
  • For GCC, use -O3

The easiest way to check if the compiler did the optimization (that I know of) is perform a call that would otherwise result in a stack overflow – or looking at the assembly output. However, you cann usually just assume that the compiler did the optimization.

/EDIT: Tail call optimization for C has been 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 ++编译器做尾递归优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆