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

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...

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: What is the fastest pure-Perl implementation of XXX?
by kvale (Monsignor) on Mar 31, 2004 at 20:20 UTC
    And you are still missing my point: asymptotic analysis is all very well and good, but if you want to optimize your code for speed, you have to pay attention to the constant multipliers.

    Suppose I use exact arithmetic:

    There is an important lesson here: The algorithm with the best asymptotic behavior isn't always faster. If speed is important, decide the parameter domain in which the algorithms are to be used, and benchmark the possibilities.

    -Mark