Store the function value to prevent it from being run again


quantify

Say I have some complex function f(fvar1, ..., fvarN)like:

def f(fvar1,..., fvarN):
    return (complicated function of fvar1, ..., fvarN).

Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak:

def g(gvar1, ..., gvarM):
    return stuff * f(gvar1 * gvar2, ..., gvar5 * gvarM) - stuff * f(gvar3, gvar2, ..., gvarM)

where the parameters of finside gcan be different linear combinations gvar1, ..., gvarMof .

Since it f's a complex function, it's expensive to call f, but it's also difficult to store the value locally gsince there gare many instances fwith different parameter combinations .

Is there a way to store such a value fso that the fsame value doesn't have to be called over and over again without having to define each different instance fof the local in it g?

violet

Yes, it's called memory. The basic idea is f()to maintain some kind of data store based on incoming parameters. Then, if it is called with the same arguments , it just returns the stored value instead of recomputing it.

By removing parameter sets according to some rules, it may be necessary to limit the size of the datastore and optimize for your desired calling pattern. For example, if a number of subgroups parameters are used together to indicate the likelihood that they will be used in the future, you might want to remove the infrequently used patterns and keep those that are used more often.

For example, consider the following Python code for adding two numbers (let's pretend this is a very time-consuming operation):

import random

def addTwo(a, b):
    return a + b

for _ in range(100):
    x = random.randint(1, 5)
    y = random.randint(1, 5)
    z = addTwo(x, y)
    print(f"{x} + {y} = {z}")

You can, but of course it's less efficient if you use the same numbers you used before. You can add memos as follows.

The code will "remember" a certain number of computations (given the dictionary, possibly random, but I can't guarantee that). If it gets an already known pair, it just returns the cached value.

Otherwise, it computes the value, stores it in a cache, and ensures that said cache doesn't grow too large:

import random, time

# Cache, and the stats for it.

(pairToSumMap, cached, calculated) = ({}, 0, 0)

def addTwo(a, b):
    global pairToSumMap, cached, calculated

    # Attempt two different cache lookups first (a:b, b:a).

    sum = None
    try:
        sum = pairToSumMap[f"{a}:{b}"]
    except:
        try:
            sum = pairToSumMap[f"{b}:{a}"]
        except:
            pass

    # Found in cache, return.

    if sum is not None:
        print("Using cached value: ", end ="")
        cached += 1
        return sum

    # Not found, calculate and add to cache (with limited cache size).

    print("Calculating value: ", end="")
    calculated += 1

    time.sleep(1) ; sum = a + b # Make expensive.

    if len(pairToSumMap) > 10:
        del pairToSumMap[list(pairToSumMap.keys())[0]]
    pairToSumMap[f"{a}:{b}"] = sum
    return sum

for _ in range(100):
    x = random.randint(1, 5)
    y = random.randint(1, 5)
    z = addTwo(x, y)
    print(f"{x} + {y} = {z}")

print(f"Calculated {calculated}, cached {cached}")

You'll also see that I've also added cached/computed information, including the last stats line that shows the cache in action, for example:

Calculated 29, cached 71

I also made the computation an expensive operation so you can see it in action (in terms of output speed). The cached will be returned immediately, and computing the sum will take a second.

Related


Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Store the function value to prevent it from being run again

quantify Say I have some complex function f(fvar1, ..., fvarN)like: def f(fvar1,..., fvarN): return (complicated function of fvar1, ..., fvarN). Now, the function g(gvar1, ..., gvarM)has an expression represented by, f(fvar1, ..., fvarN)so to speak: def g

Prevent a function or file from being run by nose?

Greg Saab Nose will automatically run any functions it finds in the project test_*. So, for example, if there is a function called: """ test_server_setup.py sets up a pristine database to use for testing. DO NOT RUN ON PROD """ def test_server_init(): dro

Prevent a function or file from being run by nose?

Greg Saab Nose will automatically run any functions it finds in the project test_*. So, for example, if there is a function called: """ test_server_setup.py sets up a pristine database to use for testing. DO NOT RUN ON PROD """ def test_server_init(): dro

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

Prevent dpm from being enabled again

Degil I have manjaro linux and I want to disable dpms permanently. I have xautolock which enables screen lock (i3lock) after a while, which also puts my monitor to sleep. I know I can "disable" DPMS, xset -dpmsbut after a reboot or during a suspend it will be

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

prevent function from being called

Sylvia So I have a function that measures connection speed and I run it every 10 seconds (without clicking anywhere). In one of the slower cases, if true, call another function to open a popup with a message. My problem is that the speed function is called eve

Prevent xmlhttp function from running on keyup again

Hammad Maqsood: So I have a keyup function that retrieves data from a MySQL table and then displays it. However, if the letters are changed, it will retrieve the data again. See the image below , the current state and the inspector All it should do is replace

Prevent xmlhttp function from running on keyup again

Hammad Maqsood: So I have a keyup function that retrieves data from a MySQL table and then displays it. However, if the letters are changed, it will retrieve the data again. See the image below , the current state and the inspector All it should do is replace

Prevent functions or files from being run by nose?

Greg Saab Nose will automatically run any functions it finds in the project test_*. So, for example, if there is a function called: """ test_server_setup.py sets up a pristine database to use for testing. DO NOT RUN ON PROD """ def test_server_init(): dro

Prevent function from being triggered multiple times

anthracite I'm trying to prevent a function from running multiple times when a condition is met $(window).scroll(function() { if (scrollvalue === wHeight) { function() } }); I want the whole thing to only run the first time and then be ignored

Prevent function from being called twice in a row

MM1 I'm learning about decorators and have an assignment that requires me to create a decorator that prevents a function from being called twice in a row. If the same function is called again, it should return None I can't seem to understand how it actually wo

Prevent function from being called multiple times

Luke I'm building a file upload component that allows you to pause/resume file uploads. The standard way to do this seems to be to break up the file into chunks on the client machine, and send the chunks along with the bookkeeping information to the server, wh

Prevent function from being called multiple times

Luke I'm building a file upload component that allows you to pause/resume file uploads. The standard way to do this seems to be to break up the file into chunks on the client machine, and send the chunks along with the bookkeeping information to the server, wh

Prevent function from being called twice in a row

MM1 I'm learning about decorators and have an assignment that requires me to create a decorator that prevents a function from being called twice in a row. If the same function is called again, it should return None I can't seem to understand how it actually wo

Prevent function from being triggered multiple times

anthracite I'm trying to prevent a function from running multiple times when a condition is met $(window).scroll(function() { if (scrollvalue === wHeight) { function() } }); I want the whole thing to only run the first time and then be ignored

Prevent function from being called twice in a row

MM1 I'm learning about decorators and have an assignment that requires me to create a decorator that prevents a function from being called twice in a row. If the same function is called again, it should return None I can't seem to understand how it actually wo