[New-bugs-announce] [issue43040] random.py randrange() is very slow if the range is a power of 2.

Donovan Baarda report at bugs.python.org
Wed Jan 27 08:07:13 EST 2021


New submission from Donovan Baarda <abo at minkirri.apana.org.au>:

I encountered a very significant slowdown migrating some code from python2.7 to python3.9 that I tracked down to randrange() being slow. After digging I noticed that _randbelow_with_getrandbits() calls getrandbits() 2x as many times, and asks for one more bit each time, than is actually required if the range requested is a power of 2.

I have a GitHub PR on the way to fix this...

----------
components: Library (Lib)
messages: 385769
nosy: abo
priority: normal
severity: normal
status: open
title: random.py randrange() is very slow if the range is a power of 2.
type: performance
versions: Python 3.10, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43040>
_______________________________________


More information about the New-bugs-announce mailing list