diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-04 10:18:35 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-01-04 10:18:35 (GMT) |
commit | d8a0bac8f761a72661fa464b34517df5294499d2 (patch) | |
tree | c28d30321197582c0ad09cae3da6f5bf57251bbd | |
parent | ca6142948ed1cb508c97bcaf21c57e3d337a0014 (diff) | |
download | cpython-d8a0bac8f761a72661fa464b34517df5294499d2.zip cpython-d8a0bac8f761a72661fa464b34517df5294499d2.tar.gz cpython-d8a0bac8f761a72661fa464b34517df5294499d2.tar.bz2 |
Issue #16674: random.getrandbits() is now 20-40% faster for small integers.
-rw-r--r-- | Misc/NEWS | 2 | ||||
-rw-r--r-- | Modules/_randommodule.c | 3 |
2 files changed, 5 insertions, 0 deletions
@@ -201,6 +201,8 @@ Core and Builtins Library ------- +- Issue #16674: random.getrandbits() is now 20-40% faster for small integers. + - Issue #16009: JSON error messages now provide more information. - Issue #16828: Fix error incorrectly raised by bz2.compress(b'') and diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index d8927c0..a729817 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) { |