#! /usr/bin/perl -w use strict; use Benchmark; use Algorithm::Loops qw(NestedLoops); use vars qw(@chars $loop_cnt $printit); $loop_cnt = 3; @chars = map chr, 0x21 .. 0x7e; # @chars = ("a" .. "c"); sub printit { print @_ if $printit; } #------------------------------------------------ sub doit4 { my $x = 1; while ($x <= $loop_cnt) { NestedLoops( [ map \@chars, 1 .. $x++ ], sub { printit join "", @_, "\n" } ); } } #------------------------------------------------ sub ret_iter3 { my $accum = shift; my $range = shift; if ($range) { ret_iter3( [@$accum, $_], @_ ) for @$range } else { printit join "", @$accum, "\n" } } sub nested_for3 { ret_iter3 [], @_; } sub doit3 { my $x = 1; while ($x <= $loop_cnt) { nested_for3 map \@chars, 1 .. $x++; } } #------------------------------------------------ sub ret_iter2 { my $accum = shift; my $range = shift; if (@_) { for (@$range) { push @$accum, $_; ret_iter2($accum, @_); pop @$accum; } } else { printit join "", @$accum, $_, "\n" for @$range; } } sub nested_for2 { ret_iter2 [], @_; } sub doit2 { my $x = 1; while ($x <= $loop_cnt) { nested_for2 map \@chars, 1 .. $x++; } } #------------------------------------------------ sub doit1 { my $x = 0; while (++$x <= $loop_cnt) { nested_for1( sub {printit join "", @_, "\n";} , map \@chars, 1..$x ); # nested_for1( # sub {printit join "", reverse(@_), "\n";} # , reverse(map \@chars, 1..$x) # ); } } sub nested_for1 { ret_iter(@_)->(); } sub ret_iter { my $fn = shift; my $range = shift; my $sub = sub {$fn->($_, @_) for @$range}; return @_ ? ret_iter($sub, @_) : $sub; } #------------------------------------------------ # $printit = 1; # doit1; # doit2; # doit3; # doit4; # exit; timethese (-10, { doit1 => 'doit1', doit2 => 'doit2', doit3 => 'doit3', doit4 => 'doit4', });