Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of expo by multiplying previous result #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions backoff/_wait_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def expo(
"""
# Advance past initial .send() call
yield # type: ignore[misc]
n = 0
base_n: float = 1
while True:
a = factor * base ** n
a = factor * base_n
if max_value is None or a < max_value:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This max_value condition doesn't work properly with your changes, it needs to be updated a bit, like this, but still 24x faster than the original:

    base_n = 1
    while True:
        a = factor * base_n
        if max_value is None or a < max_value:
            yield a
            base_n *= base

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks.

yield a
n += 1
base_n *= base
else:
yield max_value

Expand Down
8 changes: 8 additions & 0 deletions tests/test_wait_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def test_expo_max_value():
assert expect == next(gen)


def test_expo_max_value_factor():
gen = backoff.expo(factor=3, max_value=2 ** 4)
gen.send(None)
expected = [3 * 1, 3 * 2, 3 * 4, 16, 16, 16, 16]
for expect in expected:
assert expect == next(gen)


def test_fibo():
gen = backoff.fibo()
gen.send(None)
Expand Down