Hi All,
I wrote some code that looks like this:
def get(x, y)
@cachedResults.set(x,y, Math.hypot(x, y)) if @cachedResults.get(x,y).nil?
@cachedResults.get(x,y)
end
Where @cachedResults contained a 2D Array class i wrote (in a few minutes) and the purpose of this function is to make sure that I never have to call Math.hypot twice for any given (x,y). [This could be optimised further using symmetry and other things but whatever]
So I called the function and let it run 160000 times; it ran in just over 15 seconds. Then, to see how much faster it was than the non Memoized version, i changed the code to this:
def get(x, y)
Math.hypot(x, y)
end
And, much to my surprise, it took just over 15 seconds to run again. The exact same time. So my question is, are the maths functions in ruby naturally Memoized? And, if so, to what extent is ruby Memoized?
(If not then why do you think I am getting this result consistently?)