-
-
Notifications
You must be signed in to change notification settings - Fork 599
Speed up binomial #39891
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
Merged
Merged
Speed up binomial #39891
Conversation
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
Documentation preview for this PR (built with commit 3cf1002; changes) is ready! 🎉 |
tscrim
approved these changes
Apr 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is quite an impressive speedup for such a minor change. LGTM.
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Apr 10, 2025
sagemathgh-39891: Speed up binomial As first observed in sagemath#39379, when `n` and `k` are small sage integers, `binomial(n, k)` are much slower than `n.binomial(k)`. This pull request fixes it. Now `%timeit binomial(20, 10)` is only twice slower than `%timeit 20.binomial(10)` — previously it was **30** times slower. There's also a minor patch where some code is skipped when all arguments are instances of `Element`. It is certainly a more common case for the input to be `Element` than otherwise, so we optimize for it. Another minor patch involves storing imported modules as C global variables instead of re-import every time. I think using not-overriding `_method_arguments` as an indicator of "don't call the method" (then silently catch `TypeError` and throw it away) is rather bad design, but this is not in scope here. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: sagemath#39891 Reported by: user202729 Reviewer(s): Travis Scrimshaw
vbraun
pushed a commit
to vbraun/sage
that referenced
this pull request
Apr 13, 2025
sagemathgh-39891: Speed up binomial As first observed in sagemath#39379, when `n` and `k` are small sage integers, `binomial(n, k)` are much slower than `n.binomial(k)`. This pull request fixes it. Now `%timeit binomial(20, 10)` is only twice slower than `%timeit 20.binomial(10)` — previously it was **30** times slower. There's also a minor patch where some code is skipped when all arguments are instances of `Element`. It is certainly a more common case for the input to be `Element` than otherwise, so we optimize for it. Another minor patch involves storing imported modules as C global variables instead of re-import every time. I think using not-overriding `_method_arguments` as an indicator of "don't call the method" (then silently catch `TypeError` and throw it away) is rather bad design, but this is not in scope here. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: sagemath#39891 Reported by: user202729 Reviewer(s): Travis Scrimshaw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As first observed in #39379, when
n
andk
are small sage integers,binomial(n, k)
are much slower thann.binomial(k)
.This pull request fixes it. Now
%timeit binomial(20, 10)
is only twice slower than%timeit 20.binomial(10)
— previously it was 30 times slower.There's also a minor patch where some code is skipped when all arguments are instances of
Element
. It is certainly a more common case for the input to beElement
than otherwise, so we optimize for it.Another minor patch involves storing imported modules as C global variables instead of re-import every time.
I think using not-overriding
_method_arguments
as an indicator of "don't call the method"(then silently catch
TypeError
and throw it away) is rather bad design,but this is not in scope here.
📝 Checklist