in reply to Re^4: Generating lists of strings
in thread Generating lists of strings
What does that have to do with anything?
Everything!
First: Had I found the NestedLoops() syntax intelligable, I might never have sought my own solution, but as is, I would never have come up with the right syntax to do what you've done.
Second: The application I originally wrote nFor() for needed to be as efficient as possible:
c:\test>junk94 Took 0.557000 c:\test>nfor Took 0.09644
Given the very essence of the problems these routines are designed to handle is deeply nested algorithms, that nearly 6x difference can become very significant. Plus, if I need to tweak the function for specific purposes, my code is infinitely simpler to modify than NestedLoops().
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 'CO +DE'; 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Generating lists of strings
by ikegami (Patriarch) on Jan 24, 2010 at 22:48 UTC | |
by BrowserUk (Patriarch) on Jan 24, 2010 at 23:08 UTC | |
by ikegami (Patriarch) on Jan 24, 2010 at 23:21 UTC | |
by BrowserUk (Patriarch) on Jan 25, 2010 at 00:07 UTC | |
by ikegami (Patriarch) on Jan 25, 2010 at 02:55 UTC | |
|