classification
Title: Avoid reversed() in Random.shuffle()
Type: performance Stage: patch review
Components: Library (Lib) Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: rhettinger, vstinner
Priority: normal Keywords: patch

Created on 2009-05-10 17:29 by vstinner, last changed 2009-05-11 07:44 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
shuffle.patch vstinner, 2009-05-10 17:29
Messages (3)
msg87531 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-05-10 17:29
reversed(xrange(1, len(x))) is inefficient, xrange(len(x) - 1, 0, -1) 
gives exactly the same sequence but avoid reversed(). See attached 
patch. Don't expect amazing speedup.
msg87563 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-05-11 07:12
-1 on this patch.  Reversed has a very low overhead.  Readability if
more important.  The current code is self-evidently correct but the
patched code is less obviously so.
msg87565 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-05-11 07:44
FWIW, the inefficiency is only in the loop setup, the time to call
reversed() and __reversed__().  The inner loop runs at the same speed
because xrange provides a __reversed__ iterator.

Please do not go through the standard library making these minor tweaks
without making sure there is a significant measured speed-up and do
consider the readability issue.
History
Date User Action Args
2009-05-11 07:44:24rhettingersetstatus: open -> closed
resolution: rejected
messages: + msg87565
2009-05-11 07:13:00rhettingersetmessages: + msg87563
2009-05-11 00:00:47benjamin.petersonsetassignee: rhettinger

nosy: + rhettinger
2009-05-10 18:04:53pitrousetpriority: normal
stage: patch review
type: performance
versions: + Python 3.1, Python 2.7
2009-05-10 17:29:35vstinnercreate