Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I cleaned up the code to where it can recurse in a single sub, and a few other tweaks that make it a little faster. Here's a speed comparison:

$ perl ./script
Benchmark: timing 5000 iterations of new, orig...
       new: 11 wallclock secs (10.03 u +  0.00 s = 10.03 CPU) @ 498.50/s (n=5000)
      orig: 11 wallclock secs (10.98 u +  0.00 s = 10.98 CPU) @ 455.37/s (n=5000)

And the code...see the solve2() sub for the cleaned up version.
#!/usr/bin/perl use strict; use warnings; use Benchmark qw(timethese); sub solve1 { my ( $goal, $elements ) = @_; my ( @results, $RecursiveSolve, $nextValue ); $RecursiveSolve = sub { my ( $currentGoal, $included, $index ) = @_; for ( ; $index < @$elements ; ++$index ) { $nextValue = $elements->[$index]; if ( $currentGoal > 2 * $nextValue ) { $RecursiveSolve->( $currentGoal - $nextValue, [ @$included, $nextValue ], $index + 1 ); } else { if ( $currentGoal == $nextValue ) { push @results, [ @$included, $nextValue ]; } return if $nextValue >= $currentGoal; } } }; $RecursiveSolve->( $goal, [], 0 ); undef $RecursiveSolve; #Avoid memory leak from circular referen +ce return @results; } sub solve2 { my ( $goal, $list, $index, $inc, $results ) = @_; for ( $index .. @$list - 1 ) { if ( $goal > 2 * $$list[$_] ) { solve2( $goal - $$list[$_], $list, $_ + 1, [ @$inc, $$list[$_] ], $results ); } elsif ( $$list[$_] > $goal ) { return; } elsif ( $goal == $$list[$_] ) { push @$results, [ @$inc, $$list[$_] ]; last; } } return @$results; } my $goal = 869; my $tosolve = [15,43,51,56,60,67,122,152,193,204,229,271,293,301]; timethese( 5000, { 'orig' => sub { solve1( $goal, $tosolve ) }, 'new' => sub { solve2( $goal, $tosolve, 0, [], [] ) } } ); exit; #comment out to see it produces the same result print "solve2:\n"; for ( solve2( $goal, $tosolve, 0, [], [] ) ) { print join( ',', @$_ ), "\n"; } print "solve1:\n"; for ( solve1( $goal, $tosolve ) ) { print join( ',', @$_ ), "\n"; }

In reply to Re: Perl ARRAY() result by kschwab
in thread Perl ARRAY() result by pvfki

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found