Interesting. I ran same benchmark as I'd run previously. Of course I cranked up the number of iterations and took the slow recursive version out of the mix otherwise I'd have time to go have a vacation in Mexico or something!

For run #1, as more of an apples to apples deal, I flushed the cache after each subroutine call. The memoized version wound up about 1/2 the speed (1495/3478) of the iterative version which is a pretty impressive result considering how incredibly slow the non-memoized version is!

Then for run #2, I commented out the line with the "flush" and let fib_memo do its best. Not surprisingly the iterative version stayed the same while the memoized version sped up (4130/3516).

My curiosity is satisfied, but code is below if anybody wants to fiddle with it.

#!/usr/bin/perl -w use strict; use Benchmark; use Memoize qw(flush_cache memoize); use feature "state"; memoize ('fib_memo'); timethese (10000, { Normal=> q{ foreach (20..25) { #print "number $_ : fibonacci ",fibonacci($_),"\n"; my $f = fibonacci($_); } }, # Slow => # q{ # foreach (20..25) # { # #print "number $_ : fibonacci ",fibonacci_slow($_),"\n"; # my $f = fibonacci_slow($_); # } # }, Memo => q{ foreach (20..25) { #print "number $_ : fibonacci ",fib_memo($_),"\n"; my $f = fib_memo($_); flush_cache('fib_memo'); } }, } ); =prints Benchmark: timing 10000 iterations of Memo, Normal... Memo: 7 wallclock secs ( 6.69 usr + 0.00 sys = 6.69 CPU) @ 14 +95.44/s (n=10000) Normal: 3 wallclock secs ( 2.88 usr + 0.00 sys = 2.88 CPU) @ 34 +78.26/s (n=10000) Just for fun...without flush_cache('fib_memo'); Benchmark: timing 10000 iterations of Memo, Normal... Memo: 2 wallclock secs ( 2.42 usr + 0.00 sys = 2.42 CPU) @ 41 +30.52/s (n=10000) Normal: 3 wallclock secs ( 2.84 usr + 0.00 sys = 2.84 CPU) @ 35 +16.17/s (n=10000) =cut sub fibonacci { my $number = shift; my $curnum = 1; my $prevnum = 1; my $sum; if ($number ==1 || $number ==2){ return 1;} $number -= 2; while ($number--) { $sum = $curnum + $prevnum; $prevnum = $curnum; $curnum = $sum; } return $sum; } sub fibonacci_slow { my $number = shift; if ($number ==1 || $number ==2){ return 1;} return fibonacci_slow($number-1) + fibonacci_slow($number-2); } sub fib_memo { state $memo = { 1 => 1, 2 => 1 }; return $memo->{ $_[0] } //= fib_memo( $_[0] - 1 ) + fib_memo( $_[0 +] - 2 ); }

In reply to Re^3: fibonacci numbers using subroutine? by Marshall
in thread fibonacci numbers using subroutine? by derpp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.