in reply to Efficient Unique Nested Combinations

I don't know anything about Algorithm::Loops, but given your code above, there is no need for the intermediate array @list. Instead, you can work out uniqueness as you go by using the keys of a hash:

#!/usr/bin/perl -w use strict; use Algorithm::Loops qw(NestedLoops); my @symbol = ( [ 'a', 'b', 'c' ], [ 'a', 'b', 'c' ], [ 'a', 'b', 'c' ], [ 'c', 'd', 'e' ], ); my $iter = NestedLoops(\@symbol); my %seen; while (my @list = $iter->()) { my $listString = join ('',sort @list); $seen{$listString}++ } my @sortedUniq = sort (keys %seen); foreach my $sortedUniq (@sortedUniq) { print $sortedUniq,"\n"; }

Clint

UPDATE - Benchmarks added

I compared the array method to the hash method, both for data that was all the same, and for data that was completely different, the results were:

Rate array_diff array_same hash_diff hash_same array_diff 916/s -- -10% -16% -25% array_same 1012/s 11% -- -7% -18% hash_diff 1092/s 19% 8% -- -11% hash_same 1229/s 34% 21% 13% --

Code for benchmarks:

#!/usr/bin/perl -w use strict; use Algorithm::Loops qw(NestedLoops); use Benchmark qw(cmpthese); my @same = ( [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ], [ 'a', 'a', 'a' ], ); my @different = ( [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ], [ 'j', 'k', 'l' ], ); sub array { 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); } cmpthese(5000, { array_same => sub { array(\@same)}, array_diff => sub { array(\@different)}, hash_same => sub { hash(\@same)}, hash_diff => sub { hash(\@different)}, });