That benchmarks about 10% faster than the original for me, but the powerset code below is 15x faster than either of them.sub comb { map permute( '', $_, @_ ), ( 0 .. $#_ ); } sub permute { my ( $str, $depth, @chars ) = @_; map $str.$_, $depth ? (map permute( $chars[$_], $depth-1, @chars[ ( $_ + 1 ) .. ($#chars) ] ) , (0..$#chars)) : @chars; }
If you include the sort that is currently commented out, the order will be correct, but the performance will degrade, though it is still about 7x faster for A..G. When I increase the range, the advantage for powerset increases. Even with the sort in place, it is 18x faster for A..L (30x without the sort).sub powerset { my ($car, @cdr) = @_; my @cdr_powerset = @cdr ? powerset(@cdr) : (); return # sort {length $a <=> length $b or $a cmp $b} ($car, map("$car$_", @cdr_powerset), @cdr_powerset); }
In reply to Re^2: Unique Character Combinations
by Roy Johnson
in thread Unique Character Combinations
by TedPride
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |