Skip to content

gh-100239: use PyFloat_AS_DOUBLE and _PyLong_IsZero in the float / compactlong specializations#144826

Merged
Eclips4 merged 3 commits intopython:mainfrom
chris-eibl:float_exact
Feb 19, 2026
Merged

gh-100239: use PyFloat_AS_DOUBLE and _PyLong_IsZero in the float / compactlong specializations#144826
Eclips4 merged 3 commits intopython:mainfrom
chris-eibl:float_exact

Conversation

@chris-eibl
Copy link
Member

@chris-eibl chris-eibl commented Feb 14, 2026

Since we know that we deal with exact floats here.

This is just a small follow-up on #128722 and #128963.

I don't think it deserves a news entry, but I'd happily create it if deemed meaningful enough.

Microbenchmark from @iritkatriel 's PR:

Old:

>>> timeit("for i in range(10000):\n\tb = a+i", number=100000, setup="a = 1.0")
42.05178890001844
>>> timeit("for i in range(10000):\n\tb = i+a", number=100000, setup="a = 1.0")
41.31541720000678

New:

>>> timeit("for i in range(10000):\n\tb = a+i", number=100000, setup="a = 1.0")
38.316913699993165
>>> timeit("for i in range(10000):\n\tb = i+a", number=100000, setup="a = 1.0")
38.37592560000485

Fastmark results are within the noise 1, the three arithmetic benchmarks @Fidget-Spinner mentioned are a tad faster:

diff

Footnotes

  1. see my gist how I do them

know that we deal with exact floats here
@chris-eibl
Copy link
Member Author

This microbenchmark

import time


def bench():
    t = time.perf_counter()
    f = 1.0
    for i in range(60000):
        for j in range(1, 1000):
            f = f + j
            f = f - j
            f = f / j
            f = f * j
    print(time.perf_counter() - t)


if __name__ == "__main__":
    bench()

shows even better speedup:

old: 6.5 seconds
new: 5.5 seconds
@chris-eibl chris-eibl requested a review from Eclips4 February 14, 2026 20:21
@skirpichev
Copy link
Member

I don't think it deserves a news entry, but I'd happily create it if deemed meaningful enough.

This is user-visible change. So, I believe it deserves a news entry.

@@ -0,0 +1,2 @@
Speedup ``BINARY_OP_EXTEND`` for exact floats and medium-size integers by up
Copy link
Member Author

Choose a reason for hiding this comment

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

Compact ints are an implementation detail and the term medium-size integers has been chosen in news entries in the past for them.

Copy link
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

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

Thank you Chris!

@Eclips4 Eclips4 merged commit b5c8e6e into python:main Feb 19, 2026
53 checks passed
@chris-eibl chris-eibl deleted the float_exact branch February 19, 2026 19:59
bkap123 pushed a commit to bkap123/cpython that referenced this pull request Feb 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants