Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Deep recursion on subroutine

by davido (Archbishop)
on Sep 04, 2013 at 15:38 UTC ( #1052379=note: print w/replies, xml ) Need Help??

in reply to Deep recursion on subroutine

The warning can be squelched easily enough. perllexwarn should identify which class of warnings to eliminate.

But the warning could be an indication of a bigger problem (and usually is). Imagine doing a traversal of a binary tree. A *balanced* tree with a million nodes would require 20 levels of recursion to traverse (assuming a recursive approach). Deep recursion warnings come after 100 levels of recursion, if I'm not mistaken. So either the algorithm being used is inefficient, or the tree isn't balanced, or there's a bug resulting in runaway recursion.

If the tree isn't balanced, it's probably time to start looking in the datastructure cookbook again. ;)

This isn't to say that there's no legitimate use of deep recursion; there is. But Perl isn't "tail call optimized", so when you start getting to the point that you're encountering deep recursion, an iterative approach would probably be more efficient anyway. An example: A linear search of a linked list can be written beautifully with recursion, but would hit the "deep recursion" warning with just 100 nodes. One could push on beyond that, but an iterative approach would expend fewer cycles on the linear search.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052379]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-11-21 19:23 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (309 votes). Check out past polls.