Skip to content

Commit 394beb3

Browse files
Improve performance of expo by multiplying previous result (#50)
Co-authored-by: whonore <[email protected]>
1 parent ce39d61 commit 394beb3

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Use `time.monotonic` instead of `datetime.datetime.now` https://github.com/python-backoff/backoff/pull/23 (from @luccabb)
88
- Drop support for Python 3.7 https://github.com/python-backoff/backoff/pull/49 (from @edgarrmondragon)
9+
- Improve performance of expo by multiplying previous result https://github.com/python-backoff/backoff/pull/50 (from @whonore)
910

1011
## [v2.2.2] - 2025-11-17
1112

backoff/_wait_gen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def expo(
2222
"""
2323
# Advance past initial .send() call
2424
yield # type: ignore[misc]
25-
n = 0
25+
base_n: float = 1
2626
while True:
27-
a = factor * base ** n
27+
a = factor * base_n
2828
if max_value is None or a < max_value:
2929
yield a
30-
n += 1
30+
base_n *= base
3131
else:
3232
yield max_value
3333

tests/test_wait_gen.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def test_expo_max_value():
6767
assert expect == next(gen)
6868

6969

70+
def test_expo_max_value_factor():
71+
gen = backoff.expo(factor=3, max_value=2 ** 4)
72+
gen.send(None)
73+
expected = [3 * 1, 3 * 2, 3 * 4, 16, 16, 16, 16]
74+
for expect in expected:
75+
assert expect == next(gen)
76+
77+
7078
def test_fibo():
7179
gen = backoff.fibo()
7280
gen.send(None)

0 commit comments

Comments
 (0)