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

permutations is less efficient than multiset_permutations #151

Open
FedericoStra opened this issue Dec 15, 2023 · 0 comments
Open

permutations is less efficient than multiset_permutations #151

FedericoStra opened this issue Dec 15, 2023 · 0 comments

Comments

@FedericoStra
Copy link

I noticed that permutations is much less efficient than the more general multiset_permutations when they are both applied to a collection with unique elements:

julia> collect(permutations(1:8)) == collect(multiset_permutations(1:8, 8))
true

julia> @benchmark permutations(1:8) |> collect |> length
BenchmarkTools.Trial: 8 samples with 1 evaluation.
 Range (min  max):  614.237 ms  656.276 ms  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     626.957 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   632.481 ms ±  15.159 ms  ┊ GC (mean ± σ):  0.04% ± 0.12%

  █        █     ██   █          █                         █  █
  █▁▁▁▁▁▁▁▁█▁▁▁▁▁██▁▁▁█▁▁▁▁▁▁▁▁▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁█▁▁█ ▁
  614 ms           Histogram: frequency by time          656 ms <

 Memory estimate: 6.46 MiB, allocs estimate: 80643.

julia> @benchmark multiset_permutations(1:8, 8) |> collect |> length
BenchmarkTools.Trial: 1080 samples with 1 evaluation.
 Range (min  max):  3.769 ms  10.225 ms  ┊ GC (min  max):  0.00%  28.42%
 Time  (median):     4.049 ms              ┊ GC (median):     0.00%
 Time  (mean ± σ):   4.624 ms ±  1.031 ms  ┊ GC (mean ± σ):  11.09% ± 14.36%

   ▄█▆▅▄▃▁        ▁       ▁▁    ▁
  █████████▆▇▆▁▆▆▇█▇▇█▇████████▇█▇▇█▇▇▆█████▇▆▇█▅▅▄▁▄▆▄▅▁▁▅▆ █
  3.77 ms      Histogram: log(frequency) by time     7.77 ms <

 Memory estimate: 11.38 MiB, allocs estimate: 120991.

I haven't investigated the reason behind this, but I believe that it is indicative of the fact that the implementation of permutations is not optimal.

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

No branches or pull requests

1 participant