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 7526345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:48:12+00:00 2026-05-30T03:48:12+00:00

Particularly does memoization decrease performance somewhat? Is performance increase linear? I have a function

  • 0

Particularly does memoization decrease performance somewhat? Is performance increase linear?

I have a function that calls some complex math function 200,000,000 times. Without memoization (saving values/caching) it takes 1m. If I save the values-about 5,000,000 unique entries-it still takes 30s. The values are doubles, I am using my own hash function and the hash table size is about 20,000,000 (to make calculating the hash values a little bit easier).

But the complex math function is still only being run 5,000,000 times (I even checked with a counter). Why is it not running at roughly 2.5% of the speed 5,000,000/200,000,000?

Before I was using no large data structures, and now I am using a double array of size 20,000,000 just to clarify. I don’t know if that’ll make a difference.

  • 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-30T03:48:13+00:00Added an answer on May 30, 2026 at 3:48 am

    The answer to all performance questions is “benchmark it and find out”. Always. So your real-world runtime results are your answer – empirically, that’s how it behaves. You can find out the why using something like valgrind’s callgrind/cachegrind tools.

    Now, I’m going to ignore the fact you talk about hashing – I think you’re aware that if the cost of the hash computation is a significant fraction of the cost of running the function body, memoization isn’t going to help. So imagine the hash has zero cost for the purposes of the below.

    That all said, one of the biggest factors in the performance of CPU-intensive code is the cache hit rate. This is whether your processor, when it looks for information, has to go out to RAM to fetch it; if it’s already hot in the cache, the access latency is thousands of times lower and the CPU gets its work done more quickly (I’m simplifying a bit, because not all memory hits cause a pipeline stall, but this is the gist of it).

    So, although “using more memory” doesn’t directly correlate to a decrease in performance (I mean, the act of using it does, but I’m assuming you’re not talking about how much it costs to allocate objects here), when you have a wider swath of RAM in which things you need could lie, the odds of getting a cache hit are lower and that can severely lower the runtime speed of your code.

    Memoization is only a win when your function takes a nontrivial amount of time to execute. That’s usually the case, but it is a trade-off, and even under ideal circumstances, memoizing ten function calls down to five will never give you the 50% theoretical speed-up.

    This counterintuitive behavior (“but Borealid, I’m doing more work, how can it be faster?!”) is a prime example of why you should always double-check to see that an “optimization” you put in place actually boosts performance. Premature optimization is the root of all evil.

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

Sidebar

Related Questions

Does anyone know of a way to intercept dynamic method calls (particularly those that
Does Haskell standard library have a function that given a list and a predicate,
Does Intellij have any way of customizing the filtering the methods that are displayed
I have a recursive function for moving some circles on a canvas. Overed circle
Does anyone have any experience with creating database agnostic apps in Java, particularly with
I have some code that reads from an ini file in Javascript, using activex
I know there is an invoke function that does the stuff, I am overall
When I asked this previously I should have mentioned that it's particularly a light-weight
Does anyone have an IsDirty implementation they find particularly useful? Ideally I'd like to
Does HTTP PUT have advantages over HTTP POST, particularly for File Uploads? Data transfer

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.