in reply to Re^2: Syntactic sugar for tail call optimizations
in thread Syntactic sugar for tail call optimizations
I just wanted to point out that there other reasons than speed to use tail calls.
But after spotting and correcting the typo you had in in f0() it becomes more evident
use Benchmark qw(cmpthese); use diagnostics; my $limit = 10_000; sub f0 { ++$_ < $limit ? &f0 : return $_ } sub f1 { ++$_ < $limit ? goto &f1 : return $_ } sub f2 {{ ++$_ < $limit ? redo : return $_ }} cmpthese -1, { f0 => sub { $_ = 0; f0() }, f1 => sub { $_ = 0; f1() }, f2 => sub { $_ = 0; f2() }, };
Deep recursion on subroutine "main::f0" at /home/lanx/B/PL/PM/tail_cal +l.pl line 6 (#1) (W recursion) This subroutine has called itself (directly or indir +ectly) 100 times more than it has returned. This probably indicates an infinite recursion, unless you're writing strange benchmark progra +ms, in which case it indicates something else. Rate f1 f0 f2 f1 76.1/s -- -31% -73% f0 111/s 46% -- -60% f2 280/s 267% 152% --
Tail calls reuse the current call frame and thus avoid stack overflows in deep recursions.
I was able to make my system heavily swapping and unresponsive with a limit of 1000000.
Cheers Rolf
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Syntactic sugar for tail call optimizations
by ikegami (Patriarch) on May 27, 2011 at 16:25 UTC |