Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Reminder to self: must use Memoize more often!

by BrowserUk (Pope)
on Jan 12, 2013 at 13:55 UTC ( #1013040=note: print w/ replies, xml ) Need Help??


in reply to Reminder to self: must use Memoize more often!

return $fib3_cache{$n} ||= do {

I'd strongly recommend trading //= for ||=. Any function that can return 0, but costs to determine that, looses out as is.

You can get greater benefits still for functions that take low integers as inputs by using an array instead of a hash for the cache:

my @fib4_cache; sub fib4 { my $n = shift; return $fib4_cache[$n] //= do { $n < 2 ? $n : fib4($n-1) + fib4($n-2) }; } __END__ C:\test>memofib.pl Rate fib1 fib2 fib3 fib4 fib1 88.7/s -- -100% -100% -100% fib2 219835/s 247838% -- -89% -91% fib3 2073455/s 2338415% 843% -- -19% fib4 2551114/s 2877136% 1060% 23% --

And see the benefits more clearly if you do away with some of the benchmark overheads by loosing the sub wrappers (The module adds its own wrapper internally):

cmpthese(-1, { fib1 => q{ fib1 20 }, fib2 => q{ fib2 20 }, fib3 => q{ fib3 20 }, fib4 => q{ fib4 20 }, }); __END__ C:\test>memofib.pl Rate fib1 fib2 fib3 fib4 fib1 87.4/s -- -100% -100% -100% fib2 223254/s 255377% -- -89% -91% fib3 2071504/s 2370384% 828% -- -20% fib4 2579867/s 2952119% 1056% 25% --

And finally, there's a couple more % to be had:

my @fib5_cache; sub fib5 { my $n = shift; return $fib5_cache[ $n ] //= do { $n < 2 ? $n : ( $fib5_cache[ $n-1 ] //= fib5($n-1) ) + ( $fib5_cache[ $n-2 ] //= fib5($n-2) ) ## corrected; thanks +to moriitz }; } __END__ C:\test>memofib.pl Rate fib1 fib2 fib3 fib4 fib5 fib1 89.0/s -- -100% -100% -100% -100% fib2 218452/s 245352% -- -89% -91% -91% fib3 2022174/s 2272005% 826% -- -18% -20% fib4 2478824/s 2785096% 1035% 23% -- -2% fib5 2537045/s 2850512% 1061% 25% 2% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re: Reminder to self: must use Memoize more often!
Select or Download Code
Re^2: Reminder to self: must use Memoize more often!
by tobyink (Abbot) on Jan 12, 2013 at 14:40 UTC

    On my machine at least, fib6 is 4% faster than fib5:

    my @fib6_cache; sub fib6 { my $n = shift; $fib6_cache[ $n ] //= $n < 2 ? $n : ( $fib6_cache[ $n-1 ] //= fib6($n-1) ) + ( $fib6_cache[ $n-2 ] //= fib6($n-2) ); }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Fair enough. I guess we're into Benchmark vagaries. (T'was a bit unnecessary given the 1000% already gained :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1013040]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-04-18 02:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (460 votes), past polls