in reply to What is the fastest pure-Perl implementation of XXX?

2. The fastest factorial implementation is a lookup table. The next fastest (in pure perl) is an iterative scheme.

3. As the aforementioned node demonstrated, tail call recursion isn't all that fast in the current perl5. Speeding it up probably requires hacking on the core. If you need to compute factorials repeatedly in a recursive fashion, consider using memoization. Memoization builds a lookup table that could even beat the iterative scheme in certain situations.

-Mark

  • Comment on Re: What is the fastest pure-Perl implementation of XXX?

Replies are listed 'Best First'.
Re: Re: What is the fastest pure-Perl implementation of XXX?
by tilly (Archbishop) on Mar 31, 2004 at 05:39 UTC
    Correction. Iterative is not the fastest non-lookup algorithm in Perl (at least when you get to big integers). A divide-and-conquer recursive strategy can out-scale it. The reason is that in an iterative strategy you wind up doing many multiplications of a very large number with a small one. Since the large one has O(n*log(n)) digits each time, that winds up being slightly slower than quadratic.

    I would be surprised if divide and conquer were the absolute best strategy. But in pure Perl, given the overhead of Perl, I don't think that you'll easily find a faster algorithm.

      I agree that a divide and conquer algorithm will eventually win for large numbers, but there is a big multiplier to overcome at moderate numbers:
      use Benchmark qw(:all); sub recurse { #divide and conquer unshift @_, 1 if 2 != @_; my ($m, $n) = @_; if ($m < $n) { my $k = int($m/2 + $n/2); return recurse($m, $k) * recurse($k+1, $n); } else { return $m } } sub iterate { my $result = 1; $result *= $_ for 2..shift; return $result } cmpthese(10_000, { 'recurse' => sub { recurse( 160) }, 'iterate' => sub { iterate( 160) }, });
      yields
      Benchmark: timing 10000 iterations of iterate, recurse... iterate: 2 wallclock secs ( 1.53 usr + 0.00 sys = 1.53 CPU) @ 65 +35.95/s (n=10000) recurse: 23 wallclock secs (22.82 usr + 0.00 sys = 22.82 CPU) @ 43 +8.21/s (n=10000) Rate recurse iterate recurse 438/s -- -93% iterate 6536/s 1392% --
      If one uses Math::BigInt for larger factorials, then results even out a bit because of all the function calls needed to do multiplication. But if one can stick to the builtin operators and minimize function calls, it is usually a huge win in perl5.

      -Mark

        You're doing the computation in a way that entirely misses my point.

        By using the built-in operators, you are using floating point, which means that you are only keeping track of a fixed number of digits. Therefore no matter how large the factorial gets (until you overflow floating point), the iterative algorithm never slows down. Divide and conquer will never win because the problem that it is trying to alleviate - that multiplying large numbers takes a lot of operations - never arises. If you're willing to accept an approximation, then you can just use Stirling's formula and go straight to an answer.

        But if you ask for precise calculations, whether in a pure Perl implementation or in some lower-level library, my algorithmic comment holds. (Any slowness of pure Perl calls is on top of that.) Try it calculating 1000! or 10_000!. You don't even need to use Benchmark - the time for one calculation becomes painfully visible.

        I assumed, of course, that we were looking for precise calculations...