This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: random.shuffle slow on deque
Type: enhancement Stage:
Components: Documentation Versions: Python 3.0, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: benjamin.peterson, georg.brandl, loewis, phr, pitrou, rhettinger
Priority: low Keywords: patch

Created on 2008-10-14 22:03 by phr, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
dequedoc.diff rhettinger, 2008-10-16 18:34 Doc patch
Messages (7)
msg74773 - (view) Author: paul rubin (phr) Date: 2008-10-14 22:02
This is observed in Python 2.5.1, I haven't tried any later versions.

d = collections.deque(xrange(100000))
random.shuffle(d)

is quite slow.  Increasing the size to 200k, 300k, etc. shows that the
runtime increases quadratically or worse.  It's much faster to convert
the deque to a list, shuffle the list, and make a new deque from the
shuffled list.
msg74776 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2008-10-14 22:19
Why do you think this is a bug?
msg74833 - (view) Author: paul rubin (phr) Date: 2008-10-16 11:19
If it's not a bug, it is at least a surprising gotcha that should be
documented in the manual.  The collections module is described in the
library docs as "high performance container datatypes" but I could not
possibly consider the observed behavior to be high performance.
msg74834 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-10-16 12:43
Well, perhaps the deque documentation should make it clear that random
access is O(n), rather than O(1) for a list. With this information it is
easy to infer that operations such as shuffle() can be much slower on a
deque.
msg74839 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-10-16 17:13
Will add a note to the deque docs that random access is O(n).
msg74843 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-10-16 18:34
Patch attached.
msg74844 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-16 18:53
Done in r66913.
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48373
2008-10-16 18:53:40benjamin.petersonsetstatus: open -> closed
nosy: + benjamin.peterson
resolution: fixed
messages: + msg74844
2008-10-16 18:34:11rhettingersetfiles: + dequedoc.diff
assignee: rhettinger -> georg.brandl
messages: + msg74843
keywords: + patch
2008-10-16 17:13:03rhettingersetpriority: normal -> low
messages: + msg74839
2008-10-16 17:04:09rhettingersetassignee: georg.brandl -> rhettinger
nosy: + rhettinger
2008-10-16 12:43:13pitrousetversions: + Python 2.6, Python 3.0, - Python 2.5
nosy: + georg.brandl, pitrou
messages: + msg74834
priority: normal
assignee: georg.brandl
components: + Documentation, - Library (Lib)
type: enhancement
2008-10-16 11:19:57phrsetmessages: + msg74833
2008-10-14 22:19:20loewissetnosy: + loewis
messages: + msg74776
2008-10-14 22:03:00phrcreate