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 |