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 · 2622 次点击
    这是一个创建于 2337 天前的主题,其中的信息可能已经有所发展或是发生改变。

    腾讯云最新优惠活动来了:云产品限时1折,云服务器低至88元/年 ,点击这里立即抢购:9i0i.cn/qcloud,更有2860元代金券免费领取,付款直接抵现金用,点击这里立即领取:9i0i.cn/qcloudquan

    (福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

    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   ·   实用小工具   ·   6290 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 03:23 · PVG 11:23 · LAX 20:23 · JFK 23:23
    Developed with CodeLauncher
    ? Do have faith in what you're doing.


    http://www.vxiaotou.com