求优化可能

2020-11-15 12:46:19 +08:00
 imn1
一个需求现在写了两种方法实现
一个 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 那样无聊就是了
1791 次点击
所在节点    Python
8 条回复
xuanbg
2020-11-15 12:56:32 +08:00
ArrayList 用 get(i)方法按下标取难道不是最快的?
black11black
2020-11-15 13:09:04 +08:00
末项直觉想到 deque,不好使的话大概就是迭代器底层问题了吧,试试 cython 插件?
imn1
2020-11-15 13:32:44 +08:00
@xuanbg
我试试

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

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

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

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

timeit 0.013 ,略比 reduce 慢一点点,但比之前好很多了
不过重点是学习了这种快速提取的写法,谢了
xuanbg
2020-11-15 14:32:58 +08:00
如果你的需求是取最后元素,不妨抛弃数组改用栈。最后一个永远在顶部。
imn1
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 差不多,不过以后从迭代器提取中间元素应该很有用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/725424

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX