开发者

Are there any benchmarks showing good performance of `collections.deque`?

开发者 https://www.devze.com 2023-02-18 17:00 出处:网络
I was always intrigued by Python\'s collections.deque object. It seems to be like a list, except that adding/deleting items in the beginning is faster than in a list.

I was always intrigued by Python's collections.deque object. It seems to be like a list, except that adding/deleting items in the beginning is faster than in a list.

This makes me want to replace list with deque in various places in my code where I have a lis开发者_StackOverflow中文版t that I do left pops on. So my question: Did anyone ever benchmark deque against list in such scenarios?


I just did a quick google search, and found two sources with code and numbers:

A mailing-list post: http://coding.derkeiler.com/Archive/Python/comp.lang.python/2010-01/msg02138.html

A blog post: http://txzone.net/2010/04/python-is-x-is-better-than-y-round-1-deque-vs-list/

It looks like a list is slightly faster than a deque for most operations, but a deque destroys a list (2 orders of magnitude for a 100,000 element list) at .pop[0].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号