#!/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% --