8000 log2() is faster than log() (#95214) · python/cpython@eb9c8a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit eb9c8a8

Browse files
authored
log2() is faster than log() (#95214)
1 parent 0047447 commit eb9c8a8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Lib/random.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
5151
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
5252
from math import tau as TWOPI, floor as _floor, isfinite as _isfinite
53-
from math import lgamma as _lgamma, fabs as _fabs
53+
from math import lgamma as _lgamma, fabs as _fabs, log2 as _log2
5454
from os import urandom as _urandom
5555
from _collections_abc import Sequence as _Sequence
5656
from operator import index as _index
@@ -764,11 +764,11 @@ def binomialvariate(self, n=1, p=0.5):
764764
# BG: Geometric method by Devroye with running time of O(np).
765765
# https://dl.acm.org/doi/pdf/10.1145/42372.42381
766766
x = y = 0
767-
c = _log(1.0 - p)
767+
c = _log2(1.0 - p)
768768
if not c:
769769
return x
770770
while True:
771-
y += _floor(_log(random()) / c) + 1
771+
y += _floor(_log2(random()) / c) + 1
772772
if y > n:
773773
return x
774774
x += 1

0 commit comments

Comments
 (0)
0