in reply to Re: Re^4: Factorial algorithm execution time
in thread Factorial algorithm execution time

I wasn't calling it a stupid approach. :) It just seems needlessly complicated. Keeping numbers small to accelerate this algorithm hadn't occured to me before you mentioned it.

I used the overloading interface because calling bmul made Perl complain Can't call method "bmul" without a package or object reference and I was too lazy to figure it out at the time. Thanks for nagging; I took another look at the docs. Fixing it didn't really change anything though.

#!/usr/bin/perl -w use strict; use Benchmark; use Math::BigInt; sub fact7b { my @factor = map Math::BigInt->new($_), (2 .. shift); while(@factor > 1) { my @pair = splice @factor, 0, @factor / 2; $_ = $_ * pop @pair for @factor[0..$#pair]; } return shift @factor; } sub fact7c { my @factor = map Math::BigInt->new($_), (2 .. shift); while(@factor > 1) { my @pair = splice @factor, 0, @factor / 2; $_ = Math::BigInt->new($_->bmul(pop @pair)) for @factor[0..$#p +air]; } return shift @factor; } timethese 100, { f7b => sub { fact7b 1500 }, f7c => sub { fact7c 1500 }, }; __END__ Benchmark: timing 100 iterations of f7b, f7c... f7b: 388 wallclock secs (381.29 usr + 1.90 sys = 383.19 CPU) @ + 0.26/s (n=100) f7c: 387 wallclock secs (380.56 usr + 1.89 sys = 382.45 CPU) @ + 0.26/s (n=100)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re^6: Factorial algorithm execution time
by Anonymous Monk on Oct 19, 2002 at 15:38 UTC
    There is a measurable, if minor improvement. Which is about what your algorithm offers over mine.

    The reason that it is so small is that the connection between size and improvement is not that strong. You only save when you manage to reduce how many "digits" are involved, and at least with Perl 5.6's implementation of Math::BigInt, digits are of size 1e5. Making sure that only a few multiplications have huge numbers is most of what you can save. Multiplying big numbers is a bottleneck. You cannot avoid the bottleneck much more. Next you should speed it up.

    If you want to try a pure Perl solution, I suggest implementing Karatsuba multiplication. It isn't nearly as fast as Fourier transforms, but it is easier to understand and implement.