summaryrefslogtreecommitdiff
path: root/Modules/_randommodule.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-01-04 12:18:35 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2013-01-04 12:18:35 +0200
commitd8a0bac8f761a72661fa464b34517df5294499d2 (patch)
treec28d30321197582c0ad09cae3da6f5bf57251bbd /Modules/_randommodule.c
parentca6142948ed1cb508c97bcaf21c57e3d337a0014 (diff)
downloadcpython-git-d8a0bac8f761a72661fa464b34517df5294499d2.tar.gz
Issue #16674: random.getrandbits() is now 20-40% faster for small integers.
Diffstat (limited to 'Modules/_randommodule.c')
-rw-r--r--Modules/_randommodule.c3
1 files changed, 3 insertions, 0 deletions
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index d8927c05b5..a729817f3d 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -360,6 +360,9 @@ random_getrandbits(RandomObject *self, PyObject *args)
return NULL;
}
+ if (k <= 32) /* Fast path */
+ return PyLong_FromUnsignedLong(genrand_int32(self) >> (32 - k));
+
bytes = ((k - 1) / 32 + 1) * 4;
bytearray = (unsigned char *)PyMem_Malloc(bytes);
if (bytearray == NULL) {