FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-111139: Optimize math.gcd(int, int) by vstinner · Pull Request #113887 · python/cpython · GitHub

Repository navigation

gh-111139: Optimize math.gcd(int, int) - #113887

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:gcd
Jan 10, 2024
Merged

vstinner merged 2 commits into
python:mainfrom
vstinner:gcd

Conversation

vstinner commented Jan 10, 2024 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

Add a fast-path for the common case.

Benchmark:

python -m pyperf timeit \
    -s 'import math; gcd=math.gcd; x=2*3; y=3*5' \
    'gcd(x,y)'

Result: 1.07x faster (-3.4 ns)

Mean +- std dev: 52.6 ns +- 4.0 ns -> 49.2 ns +- 0.4 ns: 1.07x faster

Add a fast-path for the common case.

Benchmark:

    python -m pyperf timeit \
        -s 'import math; gcd=math.gcd; x=2*3; y=3*5' \
        'gcd(x,y)'

Result: 1.07x faster (-3.4 ns)

    Mean +- std dev: 52.6 ns +- 4.0 ns -> 49.2 ns +- 0.4 ns: 1.07x faster

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: Would you mind to review this change?

vstinner added the skip news label

I don't think that it's worth it to document such minor micro-optimization.

scoder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Approved. This is the one use case for math.gcd(). Everything else is just random noise.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM.

For larger number the difference should be much lesser than 7%.

Comment thread Modules/mathmodule.c Outdated
vstinner merged commit 93930ea into python:main Jan 10, 2024
vstinner deleted the gcd branch January 10, 2024 15:38
kulikjak pushed a commit to kulikjak/cpython that referenced this pull request Jan 22, 2024
Add a fast-path for the common case.

Benchmark:

    python -m pyperf timeit \
        -s 'import math; gcd=math.gcd; x=2*3; y=3*5' \
        'gcd(x,y)'

Result: 1.07x faster (-3.4 ns)

    Mean +- std dev: 52.6 ns +- 4.0 ns -> 49.2 ns +- 0.4 ns: 1.07x faster
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Add a fast-path for the common case.

Benchmark:

    python -m pyperf timeit \
        -s 'import math; gcd=math.gcd; x=2*3; y=3*5' \
        'gcd(x,y)'

Result: 1.07x faster (-3.4 ns)

    Mean +- std dev: 52.6 ns +- 4.0 ns -> 49.2 ns +- 0.4 ns: 1.07x faster
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
Add a fast-path for the common case.

Benchmark:

    python -m pyperf timeit \
        -s 'import math; gcd=math.gcd; x=2*3; y=3*5' \
        'gcd(x,y)'

Result: 1.07x faster (-3.4 ns)

    Mean +- std dev: 52.6 ns +- 4.0 ns -> 49.2 ns +- 0.4 ns: 1.07x faster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL