[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Nuby Tip for the Day : Memoization

John Carter

11/1/2004 3:58:00 AM

6 Answers

Joel VanderWerf

11/1/2004 4:12:00 AM

0

Just a little correction to a worthy post...

John Carter wrote:
...
> class BigExpensiveMadeOften
> def initialize( unique_name)
> # Big expensive computation
> end
>
> @@memo = Hash.new {|hash,key|
> result = BigExpensiveMadeOften.new( unique_name)
^^^^^^^^^^^
should be "key"



John Carter

11/1/2004 5:02:00 AM

0

Florian Frank

11/1/2004 5:10:00 AM

0

On 2004-11-01 12:58:27 +0900, John Carter wrote:
> Really an easy optimization to do.

Yep. You could also make it transparent for the clients of your class:

class BigExpensiveMadeOften
def initialize(unique_name)
# Big expensive computation
end

@@memo = Hash.new { |hash,key|
result = BigExpensiveMadeOften.allocate
result.__send__(:initialize, key)
hash[key] = result
}

def BigExpensiveMadeOften.new(unique_name)
@@memo[unique_name]
end
end

foo = BigExpensiveMadeOften.new('foo')
bar = BigExpensiveMadeOften.new('bar')
p foo
p bar
foo = BigExpensiveMadeOften.new('foo')
p foo

--
Florian Frank


Brian Mitchell

11/1/2004 7:55:00 AM

0

On Mon, 1 Nov 2004 14:01:34 +0900, John Carter <john.carter@tait.co.nz> wrote:
> On Mon, 1 Nov 2004, Joel VanderWerf wrote:
>
> > Just a little correction to a worthy post...
>
> thanks
>
> > John Carter wrote:
> > ...
> >> class BigExpensiveMadeOften
> >> def initialize( unique_name)
> >> # Big expensive computation
> >> end
> >>
>
> make that
> @@memo = Hash.new {|hash,key| hash[key] = BigExpensiveMadeOften.new( key)}
>
> Reminder for the day for not so Nubies...
>
> Every reference to a string is a String.new, so sometimes factoring out string constants out of inner loops can be a big win...
>

Possibly you meant a string literal. A reference is what the variable
ends up holding.

Brian Mitchell


gabriele renzi

11/1/2004 9:33:00 AM

0

Florian Frank ha scritto:
> On 2004-11-01 12:58:27 +0900, John Carter wrote:
>
>>Really an easy optimization to do.
>
>
> Yep. You could also make it transparent for the clients of your class:

and you can write your own memoize() method or Memoized class, to
transform a method/class into what you want transparently

Michael Neumann

11/1/2004 11:49:00 AM

0

On Mon, Nov 01, 2004 at 06:33:50PM +0900, gabriele renzi wrote:
> Florian Frank ha scritto:
> >On 2004-11-01 12:58:27 +0900, John Carter wrote:
> >
> >>Really an easy optimization to do.
> >
> >
> >Yep. You could also make it transparent for the clients of your class:
>
> and you can write your own memoize() method or Memoized class, to
> transform a method/class into what you want transparently

This is how I did it:

http://ntecs.de/blog/Blog/PerformanceQuantenS...

I think Robert Feldt's implementation in AspectR (?) is much more
advanced, in that each class can have it's own cache.

Regards,

Michael