Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 list that I do left pops on. So my question: Did anyone ever benchmark deque against list in such scenarios?

like image 604
Ram Rachum Avatar asked Mar 19 '11 20:03

Ram Rachum


1 Answers

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].

like image 168
Buttons840 Avatar answered Oct 20 '22 16:10

Buttons840