[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

memoize (reposting via ML

Hal E. Fulton

2/1/2006 1:23:00 AM

(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 Berger --
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



15 Answers

MenTaLguY

2/1/2006 1:58:00 AM

0

On Wed, 2006-02-01 at 10:22 +0900, Hal Fulton 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



Hal E. Fulton

2/1/2006 2:07:00 AM

0

MenTaLguY wrote:
> On Wed, 2006-02-01 at 10:22 +0900, Hal Fulton 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 Gray, are you listening? Don't you have a memoize of
your own? What does it do with this function?


Cheers,
Hal




Brian Buckley

2/1/2006 2:19:00 AM

0

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

It is working.

--Brian Buckley

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

James Gray

2/1/2006 3:48:00 AM

0

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

> James Edward Gray, 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 Gray II

Gene Ward Smith

7/26/2008 10:51:00 PM

0

Deborah <deborah@modabelam.com> rote in
news:l97n84ddb6k962otre3igjtke8gs4ehejn@4ax.com:

> The root of all evil is the self-creation idea, but it does seem to be
> an idea Carrie embraces completely. She doesn't seem to have any idea
> of absolute truth.

And these are both very closely related to "I am God". The rejection of God's
authorship, and its replacement with self-authorship, puts you in the place
of God.

--
Change but your mind on what you want to see,
And all the world must change accordingly.

Carrie

7/26/2008 11:09:00 PM

0


"Gene" <gene@chewbacca.org> wrote in message
news:Xns9AE7955D042CFgenewardsmithsbcglob@207.115.33.102...
> "Carrie" <starchild1124@yahoo.com> rote in news:g6g4qh$bdu$1@aioe.org:
>
>> If we are all one, and one with God, and the idea of separate bodies
>> in form is illusion, how can anyone BE real to anyone else?
>
> God created your brother as eternally unified with you, and real. Because
> he
> is real to God, he is real to you. This cannot be changed, nor does it
> have
> anything to do with bodies.

Did you make that clear at the start?
I thought you meant "real" in the sense of bodies, and personalities, and
people on our computer screen.
When I go offline, you aren't all that real to me anymore.
As much as you're real to me, now.
I have seen your picture, so I have more of an idea of "you" in a
physical/form way.



>
> --
> Change but your mind on what you want to see,
> And all the world must change accordingly.


Carrie

7/26/2008 11:12:00 PM

0


"Gene" <gene@chewbacca.org> wrote in message
news:Xns9AE79683DA3BCgenewardsmithsbcglob@207.115.33.102...
> "Carrie" <starchild1124@yahoo.com> rote in news:g6g54k$d8j$1@aioe.org:
>
>> Why does it bother you to think I think "I am God"?
>
> Because the Course says that is the root of all evil, and this is a Course
> group. We should at least be able to dispose of this very fundamental
> error.

Did I ever say I think I am God?
Seems like that is something you have made whatever I said into.
Aside from that, is this the ONLY thing you see (and have ever seen)
on this "course group" that you believe isn't in line with A Course in
Miracles?
I wonder why you don't spend as much time and energy trying to
straighten out others here.


>
> --
> Change but your mind on what you want to see,
> And all the world must change accordingly.


Gene Ward Smith

7/26/2008 11:14:00 PM

0

"Carrie" <starchild1124@yahoo.com> rote in news:g6garc$b2u$1@aioe.org:

> I thought you meant "real" in the sense of bodies, and personalities, and
> people on our computer screen.
>

And if I had? My body won't go away because of what you think.


--
Change but your mind on what you want to see,
And all the world must change accordingly.

Carrie

7/26/2008 11:15:00 PM

0


"Deborah" <deborah@modabelam.com> wrote in message
news:l97n84ddb6k962otre3igjtke8gs4ehejn@4ax.com...
> On Sat, 26 Jul 2008 21:48:53 GMT, Gene <gene@chewbacca.org> wrote:
>
>>"Carrie" <starchild1124@yahoo.com> rote in news:g6g54k$d8j$1@aioe.org:
>>
>>> Why does it bother you to think I think "I am God"?
>>
>>Because the Course says that is the root of all evil, and this is a Course
>>group. We should at least be able to dispose of this very fundamental
>>error.
>
> "My mind is part of God's. I am very holy."
> "God is my life, I have no life but His."
> (Etcetera, etcetera.)
>
> The root of all evil is the self-creation idea, but it does seem to be
> an idea Carrie embraces completely. She doesn't seem to have any idea
> of absolute truth. Any idea that "My mind is part of God's" means I
> can't have any true thoughts apart from God.

I can agree with you on that.
What you see me embracing (have no idea of absolute truth) is more not
wanting to fight over beliefs. What you (or anyone) believes is true is okay
with me.
Do you really think endless fighting and putting down, making fun of,
name calling, correcting others because they don't seem to believe the way
one person does, but thinks they should is of any real value? Or course
related, for that matter.
It's like how you choose to see me, for whatever reason. That's just what
it is, your choice. It doesn't have to effect me and how I see/believe about
myself (AND my "Brothers" and/or God)
Can anyone REALLY know someone else, totally, like seeing/perceiving
through their being? Especially using words on a computer screen. We come to
our own conclusions going by what we read at any given time.

>
> Deborah (BC)


Carrie

7/26/2008 11:16:00 PM

0


"Gene" <gene@chewbacca.org> wrote in message
news:Xns9AE7A116413D8genewardsmithsbcglob@207.115.33.102...
> Deborah <deborah@modabelam.com> rote in
> news:l97n84ddb6k962otre3igjtke8gs4ehejn@4ax.com:
>
>> The root of all evil is the self-creation idea, but it does seem to be
>> an idea Carrie embraces completely. She doesn't seem to have any idea
>> of absolute truth.
>
> And these are both very closely related to "I am God". The rejection of
> God's
> authorship, and its replacement with self-authorship, puts you in the
> place
> of God.

I believe "we are all one (in Truth) and One with God.
If that's evil and somehow wrong, so be it.
You can believe I am wrong and evil and separate from you because of
it, I DON'T believe that effects me, or The Truth

>
> --
> Change but your mind on what you want to see,
> And all the world must change accordingly.