# Memoize (reposting via ML)

**URL:** <https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620>\
**Category:** Ruby\
**Created:** [February 1, 2006, 2:25am UTC](https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620 "2006-02-01T02:25:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hal\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6bbea6/32.png) [@Hal\_F](https://www.ruby-forum.com/u/Hal_F)\
**Post date:** [February 1, 2006, 2:25am UTC](https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620/1 "2006-02-01T02:25:23Z")

</div>

(Reposting via ML as gateway apparently didn’t send this through.)

I was trying to use memoize the other day (v 1.2 from Dan B. –  
is there a different/better one?). Ran into some problems…

In this dumb example, where I’m passing the same parameter every time,  
the speed should _blaze_, should it not?

This doesn’t work for me. I see no measurable speed difference in these  
three cases. What am I doing wrong?

Thanks,  
Hal

require ‘memoize’

include Memoize

def zeta(x)  
r2d = 360.0/(2.0_Math::PI) # Radians to degrees  
2.0_(Math.sin(x/r2d))\*(Math.cos(x/r2d))  
end

puts Time.now  
1000000.times { z = zeta(2.0) }  
puts Time.now

memoize(:zeta)

puts Time.now  
1000000.times { z = zeta(2.0) }  
puts Time.now

memoize(:zeta,“z.cache”)

puts Time.now  
1000000.times { z = zeta(2.0) }  
puts Time.now

---

<div class="post-metadata">

**Author:** ![Hal\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6bbea6/32.png) [@Hal\_F](https://www.ruby-forum.com/u/Hal_F)\
**Post date:** [February 1, 2006, 2:58am UTC](https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620/2 "2006-02-01T02:58:30Z")

</div>

On Wed, 2006-02-01 at 10:22 +0900, Hal F. wrote:

> This doesn’t work for me. I see no measurable speed difference in these  
> three cases. What am I doing wrong?

It may be that (relative to the overhead of method calls, etc), the  
calculation is less expensive than you suppose.

-mental

---

<div class="post-metadata">

**Author:** ![Hal\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6bbea6/32.png) [@Hal\_F](https://www.ruby-forum.com/u/Hal_F)\
**Post date:** [February 1, 2006, 3:07am UTC](https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620/3 "2006-02-01T03:07:35Z")

</div>

MenTaLguY wrote:

> On Wed, 2006-02-01 at 10:22 +0900, Hal F. wrote:
> 
> > This doesn’t work for me. I see no measurable speed difference in these  
> > three cases. What am I doing wrong?
> 
> It may be that (relative to the overhead of method calls, etc), the  
> calculation is less expensive than you suppose.

C’est possible… but with the silly hash-based implementation in  
TRW1, I saw a significant speedup of this same function. And  
memoize is not _so_ complicated…

James Edward G., are you listening? Don’t you have a memoize of  
your own? What does it do with this function?

Cheers,  
Hal

---

<div class="post-metadata">

**Author:** ![Hal\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6bbea6/32.png) [@Hal\_F](https://www.ruby-forum.com/u/Hal_F)\
**Post date:** [February 1, 2006, 3:19am UTC](https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620/4 "2006-02-01T03:19:38Z")

</div>

I added a “sleep 1” line to zeta to exaggerate the time of an uncached  
method call.

It is working.

–Brian B.

def zeta(x)  
sleep 1  
r2d = 360.0/(2.0_Math::PI) # Radians to degrees  
2.0_(Math.sin(x/r2d))\*(Math.cos(x/r2d))  
end

t1 = Time.now  
10.times { z = zeta(2.0) }  
t2 = Time.now

memoize(:zeta)

t3 = Time.now  
10.times { z = zeta(2.0) }  
t4 = Time.now

memoize(:zeta,“z.cache”)  
t5 = Time.now  
10.times { z = zeta(2.0) }  
t6 = Time.now

puts “#{t2 - t1}, #{t4 - t3}, #{t6 - t5}” # =\> 10.0, 1.0, 0.0

---

<div class="post-metadata">

**Author:** ![Hal\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/6bbea6/32.png) [@Hal\_F](https://www.ruby-forum.com/u/Hal_F)\
**Post date:** [February 1, 2006, 4:49am UTC](https://www.ruby-forum.com/t/memoize-reposting-via-ml/99620/5 "2006-02-01T04:49:57Z")

</div>

On Jan 31, 2006, at 8:07 PM, Hal F. wrote:

> James Edward G., are you listening?

Yes, I am.

> Don’t you have a memoize of your own?

It’s just a toy, but yes. I didn’t bother packaging it up though,  
since there didn’t seem to be interest in it. I’ll attach the  
library to this message, in case you want to fiddle with it.

> What does it do with this function?

Mine is maybe slightly faster, but it’s really the same story.  
Running with memoize.rb I see:

$ ruby hals\_memoize.rb  
Unmemoized:  
4.436923 seconds.  
Memoized:  
5.266704 seconds.  
Memoized (file):  
5.324959 seconds.

My library shows:

$ ruby hals\_memoize.rb  
Unmemoized:  
4.47798 seconds.  
Memoized:  
3.883453 seconds.

I have to agree with the others. The operations are just too fast.  
You’ve got to get some processing or recursion in there to see a big  
boost. Anything you can run 1,000,000 tests on without memoization  
just isn’t stressful enough to benefit from it much.

Anyway, here’s the test I ran with my code:

require ‘memoizable’

extend Memoizable

def zeta(x)  
r2d = 360.0/(2.0_Math::PI) # Radians to degrees  
2.0_(Math.sin(x/r2d))\*(Math.cos(x/r2d))  
end

puts “Unmemoized:”  
start = Time.now  
1000000.times { z = zeta(2.0) }  
puts “#{Time.now - start} seconds.”

memoize(:zeta)

puts “Memoized:”  
start = Time.now  
1000000.times { z = zeta(2.0) }  
puts “#{Time.now - start} seconds.”

**END**

And again the library is attached.

Sorry I wasn’t more help.

James Edward G. II
