Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8a0bac

Browse files
Issue #16674: random.getrandbits() is now 20-40% faster for small integers.
1 parent ca61429 commit d8a0bac

2 files changed

Lines changed: 5 additions & 0 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ Core and Builtins
201201
Library
202202
-------
203203

204+
- Issue #16674: random.getrandbits() is now 20-40% faster for small integers.
205+
204206
- Issue #16009: JSON error messages now provide more information.
205207

206208
- Issue #16828: Fix error incorrectly raised by bz2.compress(b'') and

Modules/_randommodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ random_getrandbits(RandomObject *self, PyObject *args)
360360
return NULL;
361361
}
362362

363+
if (k <= 32) /* Fast path */
364+
return PyLong_FromUnsignedLong(genrand_int32(self) >> (32 - k));
365+
363366
bytes = ((k - 1) / 32 + 1) * 4;
364367
bytearray = (unsigned char *)PyMem_Malloc(bytes);
365368
if (bytearray == NULL) {

0 commit comments

Comments
 (0)