V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lniwn
V2EX  ›  Python

Python RVO

  •  
  •   lniwn ·
    lniwn · 2017-12-19 15:29:21 +08:00 · 2587 次点击
    这是一个创建于 2310 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import sys
    
    sys.setrecursionlimit(100)
    depth = 0
    
    def main():
        global depth
        print(depth)
        depth += 1
        return main()
    
    main()
    

    经过测试发现,python 没有进行 rvo 优化,是我测试姿势问题,还是其他什么原因?望 dalao 指教。

    7 条回复    2017-12-19 16:01:28 +08:00
    QAPTEAWH
        1
    QAPTEAWH  
       2017-12-19 15:34:41 +08:00
    这个叫 TCO 吧
    jotsai
        2
    jotsai  
       2017-12-19 15:40:47 +08:00
    你想说尾递归优化吧,Python 没有
    VicYu
        3
    VicYu  
       2017-12-19 15:43:13 +08:00   ❤️ 1
    lniwn
        4
    lniwn  
    OP
       2017-12-19 15:48:11 +08:00
    @QAPTEAWH Return Value Optimization,就是对这种最后一句是 return 的,不需要保留当前环境,就不用压栈了,但是实测还是压栈了,报 RecursionError 错误。
    lniwn
        5
    lniwn  
    OP
       2017-12-19 15:54:40 +08:00
    @jotsai 对,看来名字果然是记错了,RVO 是对返回值的拷贝构造函数进行优化的。。。
    lniwn
        6
    lniwn  
    OP
       2017-12-19 15:58:28 +08:00
    @VicYu 这样看来是动态语言多返回值的锅了?
    lniwn
        7
    lniwn  
    OP
       2017-12-19 16:01:28 +08:00
    @QAPTEAWH 是的,我记错了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5458 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:44 · PVG 15:44 · LAX 00:44 · JFK 03:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.