in reply to Re: Re (tilly) 3 (faster): Ackerman vs. Perl : KO in 3 rounds
in thread Ackerman vs. Perl : KO in 3 rounds

If you understand the growth pattern properly, you will realize that A(4,3) is not running with the naive algorithm in your lifetime on a computer, and even if it did, Perl would be unable to represent the answer.

Try A(3,6), A(3,7) and A(3,8) to get an idea of relative performance.

Now if you really want a speed boost, what you want to do isn't make the recursion faster, but rather insert more and more special cases to avoid spending all of the time recursing at the tail. The following executes acceptably:

sub A { BARE_BLOCK: { if (0 == $_[0]) { return 1 + $_[1]; } elsif (1 == $_[0]) { return 2 + $_[1]; } elsif (2 == $_[0]) { return 3 + 2*$_[1]; } @_=($_[0] - 1, $_[1] ? A($_[0], $_[1]-1) : 1); redo BARE_BLOCK; } }
And here we find out why to use recursion even if Perl is bad at it. This intelligent optimization would be very hard to write if you first did the low-level optimization of avoiding any recursive calls. Intelligent algorithms beat faster execution of bad ones any time!