Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 401961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:04:54+00:00 2026-05-12T17:04:54+00:00

I wrote some code that looks like this: def get(x, y) @cachedResults.set(x,y, Math.hypot(x, y))

  • 0

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?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:04:54+00:00Added an answer on May 12, 2026 at 5:04 pm

    Does it take around 15 seconds to do anything 160000 times for you? Benchmark it on your system just returning x; it may well be that the hypot operation ( implemented in C ) is negligible than the interpreter overhead.

    For ruby 1.8.7 with khell’s memoized get method, calling the function inside a get method, and just returning x inside a get method, with 100000 iterations:

    peregrino:$ time ruby src/memoized_hypot.rb 
    
    real    0m1.714s
    user    0m1.436s
    sys 0m0.080s
    peregrino:$ time ruby src/plain_hypot.rb 
    
    real    0m0.495s
    user    0m0.364s
    sys 0m0.060s
    peregrino:$ time ruby src/empty_hypo.rb 
    
    real    0m0.369s
    user    0m0.220s
    sys 0m0.068s
    

    kheill’s memoization creates a string on every call, which is much more costly than calling the C library’s hypot function on every call.

    The difference between the calling hypot and just returning x indicates that hypot is only contributing 25% of the runtime. It’s not the code you should be optimising – instead try inlining the call to the library if you can rather than wrapping it in another method.

    peregrino:$ time ruby src/inline_hypot.rb 
    
    real    0m0.365s
    user    0m0.236s
    sys 0m0.044s
    

    which is

    100000.times{ |i| Math.hypot(i,6) }   
    

    rather than

    100000.times{ |i| foo.get(i,6) }   
    

    where foo is an object with the methods posted.


    These times were on a netbook ( Asus eeepc 900 ) which isn’t massively speedy, so it’s a bit odd that they are much faster than your times. So something else might be dominating your results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 249k
  • Answers 249k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why not Matrix.GetLength(0) May 13, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer You can also create some class with public static field… May 13, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer I think the answer is nesting, if your XML looks… May 13, 2026 at 9:06 am

Related Questions

Absolute beginner question: I have a template file index.html that looks like this: ...
The GString concept in Groovy is pretty powerful (see http://groovy.codehaus.org/Strings+and+GString ). GStrings let you
While I have been playing with Python for a few months now (just a
I'm trying to write an action generator for my Rails apps (probably the most

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.