in reply to Factorial algorithm execution time
1 * 2 * 3 * 4 * 5 = | | | | | +-------+ | +---------------+ 5 * 8 * 3 = | | +-------+ 15 * 8 = 120See fact7.
The difference is negligible for small numbers, but quickly grows to significant magnitude.#!/usr/bin/perl -w use strict; use Benchmark; use Math::BigInt; sub fact1 { my $n = Math::BigInt->new(shift); return 1 unless $n->bcmp(0); return $n->bmul(fact1($n->bsub(1))); } sub fact4 { my $n = Math::BigInt->new(1); foreach my $i (map Math::BigInt->new($_), 1..shift) { $n = Math::BigInt->new($i->bmul($n)); } return $n; } sub fact7 { my @factor = map Math::BigInt->new($_), (1 .. shift); while(@factor > 1) { my @pair = splice @factor, 1 + $#factor / 2; $_ = $_ * pop @pair for @factor[0..$#pair]; } return shift @factor; } my $fact = shift || 100; my ($f1, $f4, $f7) = (fact1($fact), fact4($fact), fact7($fact)); die "something's broke" if grep $_ != $f1, ($f4, $f7); timethese(shift || 10, { f1 => sub { fact1($fact) }, f4 => sub { fact4($fact) }, f7 => sub { fact7($fact) }, }); __END__ $ fact 600 5 Deep recursion on subroutine "main::fact1" at fact line 9. Benchmark: timing 5 iterations of f1, f4, f7... Deep recursion on subroutine "main::fact1" at fact line 9. Deep recursion on subroutine "main::fact1" at fact line 9. Deep recursion on subroutine "main::fact1" at fact line 9. Deep recursion on subroutine "main::fact1" at fact line 9. Deep recursion on subroutine "main::fact1" at fact line 9. f1: 17 wallclock secs (17.09 usr + 0.10 sys = 17.19 CPU) @ 0 +.29/s (n=5) f4: 15 wallclock secs (15.09 usr + 0.06 sys = 15.15 CPU) @ 0 +.33/s (n=5) f7: 4 wallclock secs ( 3.99 usr + 0.04 sys = 4.03 CPU) @ 1 +.24/s (n=5)
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Factorial algorithm execution time
by gri6507 (Deacon) on Oct 18, 2002 at 17:31 UTC | |
by Aristotle (Chancellor) on Oct 18, 2002 at 17:41 UTC | |
by Anonymous Monk on Oct 19, 2002 at 11:20 UTC |