c:\test>junk94 Took 0.557000 c:\test>nfor Took 0.09644 #### c:\test>type junk94.pl #! perl -slw use strict; use Time::HiRes qw[ time ]; use Algorithm::Loops qw( NestedLoops ); sub nFor(&@) { my $cb = shift; NestedLoops([ map [ 0..$_-1 ], @_ ], $cb); } my @digits = 1 .. 3; my $s = time; nFor { my $x = @digits[ @_ ]; } ( 3 ) x 10; printf "Took %.6f\n", time() - $s; c:\test>junk94 Took 0.556000 c:\test>type nfor.pl #! perl -slw use strict; use Time::HiRes qw[ time ]; sub nFor(&@) { my $code = shift; die "First argument must be a code ref" unless ref( $code ) eq 'CODE'; my @limits = @_; my @indices = ( 0 ) x @limits; for( my $i = $#limits; $i >= 0; ) { $i = $#limits; $code->( @indices ), ++$indices[ $i ] while $indices[ $i ] < $limits[ $i ]; $i = $#limits; $indices[ $i ] = 0, ++$indices[ --$i ] while $i >= 0 and $indices[ $i ] == $limits[ $i ]; } } my $s = time; my @digits = 1 .. 3; nFor { my $x = @digits[ @_ ]; } ( 3 ) x 10; printf "Took %.5f\n", time() - $s; c:\test>nfor Took 0.10450