(original) (raw)
changeset: 81278:b0926ddcab5e parent: 81276:a733e7535e2d user: Serhiy Storchaka storchaka@gmail.com date: Fri Jan 04 12🔞35 2013 +0200 files: Misc/NEWS Modules/_randommodule.c description: Issue #16674: random.getrandbits() is now 20-40% faster for small integers. diff -r a733e7535e2d -r b0926ddcab5e Misc/NEWS --- a/Misc/NEWS Thu Jan 03 20:34:58 2013 -0800 +++ b/Misc/NEWS Fri Jan 04 12🔞35 2013 +0200 @@ -201,6 +201,8 @@ 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 -r a733e7535e2d -r b0926ddcab5e Modules/_randommodule.c --- a/Modules/_randommodule.c Thu Jan 03 20:34:58 2013 -0800 +++ b/Modules/_randommodule.c Fri Jan 04 12🔞35 2013 +0200 @@ -360,6 +360,9 @@ 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) { /storchaka@gmail.com