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

  • Home
  • SEARCH
  • 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 7646521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:13:51+00:00 2026-05-31T10:13:51+00:00

I know how to do memoization in Python easily but I need a faster

  • 0

I know how to do memoization in Python easily but I need a faster way to compute them, so I am using C++. However, I have no clue how to memoize. I understand that it’s about storing values into an array or vector and then scanning for its value when retrieving, but it’d be really helpful to see how this is done so I can try its speed.

  • 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-31T10:13:52+00:00Added an answer on May 31, 2026 at 10:13 am

    Well the neatest way I can think of to do this in C++ is probably using a function object to store the memoized values. I guess this is probably slightly similar to your python decorator, although I have never really done any python. The code would look something like this:

    template <typename T, T (*calc)(T)>
    class mem {
      std::map<T,T> mem_map;
    
    public:
      T operator()(T input) {
        typename std::map<T,T>::iterator it;
    
        it = mem_map.find(input);
        if (it != mem_map.end()) {
          return it->second;
        } else {
          T output = calc(input);
          mem_map[input] = output;
          return output;
        }
      }
    };
    

    The code defines a template class that takes in a typename and a function pointer, the function operator is then defined on the class allowing it to be called. The function operator takes in an input value checks if said value is in a map, then either simply returns it from the map or calculates it (using the function pointer), adds it to the map and then returns it.

    So assuming you define some processing function like say:

    int unity(int in) { return in; }
    

    You would use the code like this:

    mem<int, unity> mem_unity;
    int y;
    y = mem_unity(10);
    

    So we define an instance of the mem class which takes our value type and processing function as template parameters, then simply call this class as a function.

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

Sidebar

Related Questions

I know, I would have thought the answer was obviously no as well, but
I know there are similar questions already on SO but none of them seem
I know the input have the maxlength, but I would like to have minlength
I know jquery have backwardscompatibility files. But if you link to this specific version:
I know that this may be common knowledge, but is there a way to
I know threre a lot of libraries dotnetopenauth,oauthbase, etc. But i need sample of
know nothing about php, but I have this script that reads a folder and
I know that lot of questions about HTML sanitizers have appeared in SO, but
Know this might be rather basic, but I been trying to figure out how
Know if it's possible to access the iPhone compass in Safari using JavaScript? I

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.