http://www.perlmonks.org?node_id=1021347


in reply to Re: Multiple Permutation handling
in thread Multiple Permutation handling

++ for clever use of glob. It actually performs quite well in this case:

Rate Algorithm::Loops glob Nested l +oops Algorithm::Loops 371/s -- -88% +-91% glob 3059/s 724% -- +-27% Nested loops 4219/s 1036% 38% + --
#!/usr/bin/env perl use warnings; use 5.014; use Algorithm::Loops qw/NestedLoops/; use Benchmark qw/:all/; my @SKU = (1..5); my @shirt = qw/T S L H/; my @size = qw/S M L XL 2X/; my @col = qw/BLU GRN WHT BLK/; say scalar algorithm_loops(); say scalar nested_loops(); say scalar do_glob(); cmpthese(-10, { 'Algorithm::Loops' => \&algorithm_loops, 'Nested loops' => \&nested_loops, 'glob' => \&do_glob, }); sub algorithm_loops { return NestedLoops([ \@SKU, \@shirt, \@size, \@col ], sub { 'SKU' . join '', @_ }); } sub nested_loops { my @r; for my $sku (@SKU) { for my $shirt (@shirt) { for my $size (@size) { for my $col (@col) { push @r, 'SKU'.$sku.$shirt.$size.$col; } } } } return @r; } sub do_glob { () = glob 'SKU{'.join(',',@SKU).'}{'. join(',',@shirt).'}{'. join(',',@size).'}{'. join(',',@col).'}'; }