Skip to content

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

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

gh-111139: Optimize math.gcd(int, int)#113887
vstinner merged 2 commits into
python:mainfrom
vstinner:gcd

Conversation

@vstinner

@vstinner vstinner commented Jan 10, 2024

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
@vstinner

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 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.

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

@serhiy-storchaka 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.

LGTM.

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

Comment thread Modules/mathmodule.c Outdated
@vstinner vstinner merged commit 93930ea into python:main Jan 10, 2024
@vstinner 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
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