Skip to content
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

Improve partitions performance #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

8d1h
Copy link

@8d1h 8d1h commented Aug 25, 2021

Here are some timings for comparison.
before

julia> @btime collect(partitions(10));
  3.995 μs (85 allocations: 6.12 KiB)

julia> @btime collect(partitions(30));
  772.853 μs (11210 allocations: 1017.45 KiB)

julia> @btime collect(partitions(50));
  35.195 ms (408454 allocations: 42.44 MiB)

after

julia> @btime collect(partitions(10));
  1.467 μs (44 allocations: 5.53 KiB)

julia> @btime collect(partitions(30));
  222.300 μs (5607 allocations: 930.16 KiB)

julia> @btime collect(partitions(50));
  10.732 ms (204229 allocations: 39.36 MiB)

This is about the same speed as JuLie.partitions or AbstractAlgebra.Generic.partitions. Also the algorithm is fairly simple and straightforward :)

Some other remarks

  • Use type parameter so one can use partitions(Int8(n)) to save some memory for large n (the partitions are generated as Vector{Int8} in this case);
  • For consistency, partitions(0) returns [Int[]] and partitions(-1) returns an empty iterator (this is treated in Fix integer partitions #82 but has not been merged);
  • Added deprecation warning for integer_partitions (which I'd suggest to replace completely with collect(partitions(n))).

* Add deprecation warning for `integer_partitions`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant