Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Misunderstanding Recursion

by ph713 (Pilgrim)
on Dec 15, 2006 at 23:05 UTC ( [id://590149]=note: print w/replies, xml ) Need Help??


in reply to Misunderstanding Recursion

As some have already said, recursion can be expensive in perl. As a real-world example, compare the algorithms in Algorithm::C3 0.01 vs 0.06:

Alg::C3 0.01

Alg::C3 0.06

The 0.01 version of the algorithm is recursive, whereas the 0.06 version is an iterative refactor. As indicated by throop above, you can see that the algorithm became much more complex and difficult to maintain and debug as a result. Adding to the ugliness is the fact that the recursive version of this algorithm cannot be transformed into a tail-recursive version, which is usually the first step in a simple transformation to iteration. The only way to transform this one was by making our own stack to simulate the call stack of the recursive version.

In this case it was worth it, as the iterative approach is noticeably faster, it's a low-level module that other big modules use, and profiling apps that use it show it to be a performance hotspot.

Transforming recursion to iteration is a performance win in Perl, but there's always the tradeoffs to consider.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590149]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found