Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

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

Depending on your data, you might save a lot of memory by changing from

$paths->[0][0][0] = [ [ 3, 0, 0 ], ]; $paths->[0][1][0] = [ [ 0, 2, 0 ], ]; ...
to
$paths->{0,0,0} = [ join($;, 3,0,0), ]; $paths->{0,1,0} = [ join($;, 0,2,0), ]; ...

For starters, you have way fewer values, and hashes are better for sparse data. You could even pack the data down if you know the range of the numbers.

sub to_key { my ($x, $y, $s) = @_; return pack 'L', ($x-MIN_X) * (MAX_Y-MIN_Y) * (MAX_S-MIN_S) + ($y-MIN_Y) * (MAX_S-MIN_S) + ($s-MIN_S) } sub fr_key { my $key = unpack 'L', $_[0]; return ( int( $key / (MAX_Y-MIN_Y) / (MAX_S-MIN_S) ) + MI +N_X, int( $key / (MAX_S-MIN_S) ) % (MAX_Y-MIN_Y) + MI +N_Y, int( $key ) % (MAX_S-MIN_S) + MI +N_S, ); } $paths->{to_key(0,0,0)} = join '', to_key(3,0,0), ); $paths->{to_key(0,1,0)} = join '', to_key(0,2,0), ); ...

fr_key( substr($branches, $i*4, 4) ) would be used instead of @{ $branches->[$i] }.


Aside from that, moving to an iterator (as you suggested) would also help. Your current function generates *all* paths before returning. Instead, it could return a path as soon as it finds one.


But before you do anything, are you sure the deep recursion is simply deep (not a problem, except you might run out of memory) and not infinite?


In reply to Re: Eliminating Recursive Tree Walking by using MJD-style Infinite Streams? by ikegami
in thread Eliminating Recursive Tree Walking by using MJD-style Infinite Streams? by jryan

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 learning in the Monastery: (3)
As of 2024-04-25 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found