推荐学习书目
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
imn1
V2EX  ›  Python

求优化可能

  •  
  •   imn1 · Nov 15, 2020 · 2229 views
    This topic created in 2032 days ago, the information mentioned may be changed or developed.
    一个需求现在写了两种方法实现
    一个 functools.reduce 实现,能直接返回目标
    一个 itertools.accumulate,返回 itertor,最后一个元素是目标

    timeit 测了一下
    reduce 需时约 0.01
    accumulate 返回 itertor 约 0.005 ,但加上 list,取[-1],就需要 0.02 ,时间翻了倍
    现在想知道 accumulate ( itertor )有没有更快提取最后元素的方法?
    试过 collections.deque [-1],只是略微比 list 快一点点,约 0.019 ,作用不大

    实现的需求,就是传入一个 functions 列表,或者参数列表,求移动计算结果,是个通用模块
    需求是什么不是重点,为什么有这样的需求也不是重点,只问 itertor 提取优化
    就权当在研究 list vs tuple 那样无聊就是了
    8 replies    2020-11-15 15:29:23 +08:00
    xuanbg
        1
    xuanbg  
       Nov 15, 2020   ❤️ 1
    ArrayList 用 get(i)方法按下标取难道不是最快的?
    black11black
        2
    black11black  
       Nov 15, 2020   ❤️ 1
    末项直觉想到 deque,不好使的话大概就是迭代器底层问题了吧,试试 cython 插件?
    imn1
        3
    imn1  
    OP
       Nov 15, 2020
    @xuanbg
    我试试

    @black11black
    有什么模块推荐?
    JeffGe
        4
    JeffGe  
       Nov 15, 2020 via Android   ❤️ 1
    for x in iterable: pass

    这个也可以取末项吧
    metamask
        5
    metamask  
       Nov 15, 2020   ❤️ 1
    @imn1 #2
    @JeffGe #3

    > accumulate 返回 itertor 约 0.005 ,但加上 list,取[-1],就需要 0.02 ,时间翻了倍

    *_, last = iterable
    imn1
        6
    imn1  
    OP
       Nov 15, 2020
    @freakxx #5
    不知道还有这种写法,学习了

    timeit 0.013 ,略比 reduce 慢一点点,但比之前好很多了
    不过重点是学习了这种快速提取的写法,谢了
    xuanbg
        7
    xuanbg  
       Nov 15, 2020   ❤️ 1
    如果你的需求是取最后元素,不妨抛弃数组改用栈。最后一个永远在顶部。
    imn1
        8
    imn1  
    OP
       Nov 15, 2020
    测试了几种写法
    *_, last = itertools.accumulate(pairs, fun) 0.013
    collections.deque(itertools.accumulate(pairs, fun), 1).pop() 0.013
    next(more_itertools.tail(1, itertools.accumulate(pairs, fun))) 0.017
    collections.deque(itertools.accumulate(pairs, fun))[-1] 0.019

    deque 时间不稳定,变化幅度大

    #7 求栈思路是最合适的,可惜 python 迭代类型不支持反向,需要更高级写法,无奈能力不够

    more_itertools.nth 没测试,应该跟 tail 差不多,不过以后从迭代器提取中间元素应该很有用
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5514 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 06:02 · PVG 14:02 · LAX 23:02 · JFK 02:02
    ♥ Do have faith in what you're doing.