-
Notifications
You must be signed in to change notification settings - Fork 59
Optimize permutations
by implementing it via multiset_permutations
#186
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
base: master
Are you sure you want to change the base?
Conversation
…ations` As it currently stands, `multiset_permutations` is more efficient than `permutations`; see JuliaMath#151. We can exploit it to optimize `permutations`.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #186 +/- ##
==========================================
- Coverage 97.22% 97.17% -0.06%
==========================================
Files 8 8
Lines 829 813 -16
==========================================
- Hits 806 790 -16
Misses 23 23 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
9ebd8fa
to
995a046
Compare
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.
This looks great. No comments
Of course I don't wish to take any credit at all for this silly PR. All the merit goes to whoever coded the fast |
Without this fix there are issues with `OffsetArray`: `eachindex` returns an `OffsetArrays.IdOffsetRange`, which causes troubles when passed to `multiset_permutations` because `f = [sum(c == x for c in a)::Int for x in m]` becomes an `OffsetArray` instead of a Vector`.
I merged I then had to apply a small fix ( |
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.
I'm glad to see this! Speedups are always great.
Code changes
As noted in #151,
multiset_permutations
is much faster thanpermutations
, so we can exploit it to optimize the latter (this was suggested in a comment here).The code does the equivalent of
but we construct the iterator manually so that we can define
eltype
or it (otherwise, withIterators.map
, type inference would deduceAny
).This closes #151; it possibly closes #185 too.
Simple benchmark
Benchmark code
Before
After