Thank you all very much for helpful comments;
I hope I was explicit enough. I meant to say that 'aab' is the same as 'baa', so to output 'aab' or 'baa' or 'aba', but not all, and to do this the most efficient way. Hereafter, "symbols" are replaced by short strings, and the problem remains the same. I believe so far the suggestion from Roy Johnson is the best:
#!/usr/bin/perl -w use strict; use Algorithm::Loops qw(NestedLoops); use Benchmark qw(cmpthese); #!/usr/bin/perl -w use strict; use Algorithm::Loops qw(NestedLoops); use Benchmark qw(cmpthese); my @symbols=( ['abc','def','ghi'], ['abc','def','ghi'], ['abc','def','ghi'], ['abc','def','ghi'], ['abc','def','ghi'], ['abc','def','ghi'], ); sub sorted { my $symbols = shift; my @all; my $iter = NestedLoops(\@symbols); my @list; while (@list = $iter->()) { my @sortedList = sort @list; my $listString = join ('',@sortedList); push @all, $listString; } my @uniq = keys %{{ map { $_ => 1 } @all }}; my @sortedUniq = sort (@uniq); } sub hash { my $symbols = shift; my $iter = NestedLoops($symbols); my %seen; while (my @list = $iter->()) { my $listString = join ('',sort @list); $seen{$listString}++ } my @sortedUniq = sort (keys %seen); } sub combo { my $symbols = shift; my $combos = NestedLoops([ $symbols[0], map { my $_hold = $_; sub { [grep {$_ ge $_[$#_]} @{$symbols[$_hold]}] } } 1..$#symbols ]); my @result; my @sortedUniq; push @sortedUniq, "@result" while @result = $combos->(); } cmpthese(1000, { sorted => sub { sorted(\@symbols) }, hash => sub { hash(\@symbols)}, combo => sub { combo(\@symbols)}, }); ##########BENCHMARK########## Rate sorted hash combo sorted 59.6/s -- -26% -94% hash 80.8/s 36% -- -91% combo 926/s 1455% 1045% --
Thanks again, best.

In reply to Re: Efficient Unique Nested Combinations by FFRANK
in thread Efficient Unique Nested Combinations by FFRANK

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.