in reply to Re: Re (tilly) 3 (faster): Ackerman vs. Perl : KO in 3 rounds
in thread Ackerman vs. Perl : KO in 3 rounds
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:
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!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; } }
|
|---|