Issue 5986: Avoid reversed() in Random.shuffle() (original) (raw)

Issue5986

Created on 2009-05-10 17:29 by vstinner, last changed 2022-04-11 14:56 by admin. 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
2022-04-11 14:56:48 admin set github: 50236
2009-05-11 07:44:24 rhettinger set status: open -> closedresolution: rejectedmessages: +
2009-05-11 07:13:00 rhettinger set messages: +
2009-05-11 00:00:47 benjamin.peterson set assignee: rhettingernosy: + rhettinger
2009-05-10 18:04:53 pitrou set priority: normalstage: patch reviewtype: performanceversions: + Python 3.1, Python 2.7
2009-05-10 17:29:35 vstinner create