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
imn1
V2EX  ›  Python

求优化可能

  •  
  •   imn1 · 2020-11-15 12:46:19 +08:00 · 1784 次点击
    这是一个创建于 1260 天前的主题,其中的信息可能已经有所发展或是发生改变。
    一个需求现在写了两种方法实现
    一个 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 条回复    2020-11-15 15:29:23 +08:00
    xuanbg
        1
    xuanbg  
       2020-11-15 12:56:32 +08:00   ❤️ 1
    ArrayList 用 get(i)方法按下标取难道不是最快的?
    black11black
        2
    black11black  
       2020-11-15 13:09:04 +08:00   ❤️ 1
    末项直觉想到 deque,不好使的话大概就是迭代器底层问题了吧,试试 cython 插件?
    imn1
        3
    imn1  
    OP
       2020-11-15 13:32:44 +08:00
    @xuanbg
    我试试

    @black11black
    有什么模块推荐?
    JeffGe
        4
    JeffGe  
       2020-11-15 13:36:39 +08:00 via Android   ❤️ 1
    for x in iterable: pass

    这个也可以取末项吧
    freakxx
        5
    freakxx  
       2020-11-15 13:47:19 +08:00   ❤️ 1
    @imn1 #2
    @JeffGe #3

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

    *_, last = iterable
    imn1
        6
    imn1  
    OP
       2020-11-15 14:11:24 +08:00
    @freakxx #5
    不知道还有这种写法,学习了

    timeit 0.013 ,略比 reduce 慢一点点,但比之前好很多了
    不过重点是学习了这种快速提取的写法,谢了
    xuanbg
        7
    xuanbg  
       2020-11-15 14:32:58 +08:00   ❤️ 1
    如果你的需求是取最后元素,不妨抛弃数组改用栈。最后一个永远在顶部。
    imn1
        8
    imn1  
    OP
       2020-11-15 15:29:23 +08:00
    测试了几种写法
    *_, 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 差不多,不过以后从迭代器提取中间元素应该很有用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2522 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 15:48 · PVG 23:48 · LAX 08:48 · JFK 11:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.