[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

perl memorize langsommere end manual memorize ?



Hej,

  I forbindelse med et perl kursus demostrerer jeg memorize modulet
vha. Fibonacci-tal med nedenstående program.
  Min undren er dog at en manual memorize er 6x gange hurtigere end
memorize modulet. Hvad kan det skyldes ?

Hilsen
Martin


output:

"
fib(34) is 5702887 and took 20.16 seconds using brute force
fib(38) is 39088169 and took 0.0013 seconds using the memorize module
fib(38) is 39088169 and took 0.0002 seconds using own memorize
"

og selvflg. programmet:

#!/usr/bin/perl -w

use Time::HiRes qw( time );
use Memoize;
memoize('fib2');

sub fib {
  my $n = shift;
  return $n if $n < 2;
  return fib($n-1) + fib($n-2);
}

sub fib2 {
  my $n = shift;
  return $n if $n < 2;
  return fib2($n-1) + fib2($n-2);
}

sub fib3 {
  my $n = shift;
  return $n if $n < 2;
  $saved{$n}=fib3($n-1)+fib3($n-2) if (!defined($saved{$n}));
  return $saved{$n};
}

$start = time(); $result=fib(34); $end = time();
printf "fib(34) is %d and took %.2f seconds using brute
force\n",$result,($end-$start);

$start = time(); $result=fib2(38); $end = time();
printf "fib(38) is %d and took %.4f seconds using the memorize
module\n",$result,($end-$start);

$start = time(); $result=fib3(38); $end = time();
printf "fib(38) is %d and took %.4f seconds using own
memorize\n",$result,($end-$start);


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2013-07-01, 02:05 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *