-
-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
Remove unnecessary logic of float __floordiv__ #83615
Comments
./python.exe -m pyperf timeit "a = 3.5" "b = a // 2" |
Is this worth optimising? Floating-point floor division is a comparatively rare operation. |
|
And on the other side, >>> 3.8 // 0.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: float divmod() I think that people expect not the current message. |
So the risk here is that by adding the floordiv fast path, the division code is duplicated, and that increases the risk of accidentally losing the invariant that Whether the optimization is worth the cost, I'm not sure. My gut feeling is not, but others may have different views. |
Okay, what if we create a common divmod function except for creating a tuple? |
@mark.dickinson I extract the common function. optimization is still work :) AS-IS: Mean +- std dev: 360 ns +- 19 ns what do you think? |
Thank you! |
Thanks, that's a nice optimization! I'm surprised that creating a tuple of 2 items, get one item directly into the C structure, and destroy the tuple is so slow (360 ns => 185 ns: 175 ns less). With my FASTCALL optimization on function calls, I recall that avoiding the creation a tuple of N items made function calls around 20 ns faster. Not 175 ns. |
Victor: I suspect there's some compiler magic going on, too; a good compiler should be able to figure out that half of the div/mod calculation is not being used, and strip it out. That wouldn't have been possible before with the tuple packing and unpacking. |
Small style question: using 5 spaces in https://github.com/python/cpython/blob/master/Objects/floatobject.c#L655-L664 after PR 18147 merged. |
@shihai1991 Good catch! Now fixed. |
Thanks good catch :) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: