Fast memoization with tuples

Radost

I want to use memoization to speed up code looking similar to (only a small number of possible values of arguments are ever called):

double MyFun(double a,double b,int c,char d)
{
    double a = cpu_intensive_pure_function_1(a,c,d);
    double b = cpu_intensive_pure_function_2(b,c,d);
    return a+b;
}

One possibility is to wrap args into a Tuple object and use Dictionary (new versions of Dotnet have hashing of tuples done for you)

Dictionary<Tuple<double,double,int,char>,double> MyFunCache = new Dictionary<Tuple<double,double,int,char>,double> ();
double MyFun(double a,double b,int c,char d)
{
    var tmp = Tuple<double,double,int,char>(a,b,c,d);
    if(MyFunCache.ContainsKey(tmp))
    {
         return MyFunCache[tmp];
    }

    double a = cpu_intensive_pure_function_1(a,c,d);
    double b = cpu_intensive_pure_function_2(b,c,d);
    return a+b;
}

But this requires creating an Tuple object every time function is called which seems wasteful, isn't there some better way? Something holding the arguments already?

Sean

You can use a ValueTuple instead. Also, remember to update the cache once you've got the computed value:

Dictionary<(double,double,int,char) ,double> MyFunCache = new Dictionary<(double,double,int,char) ,double> ();
double MyFun(double a,double b,int c,char d)
{
    var key = (a,b,c,d);
    if(MyFunCache.TryGetValue(key, out var cachedResult))
    {
         return cachedResult;
    }

    double a = cpu_intensive_pure_function_1(a,c,d);
    double b = cpu_intensive_pure_function_2(b,c,d);

    MyFunCache.Add(key, a+ b);
    return a+b;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Memoization Function In Kotlin

分類Dev

Immutable Memoization - is it possible?

分類Dev

Memoization of a Recursive Search

分類Dev

longest common subsequence using memoization

分類Dev

reuse/memoization of global polymorphic (class) values in Haskell

分類Dev

Add memoization to a recursively defined haskell function

分類Dev

Subtracting tuples

分類Dev

What "this.data = this.data || {}" means in Memoization pattern?

分類Dev

Can JavaScript memoization only be used to cache returned results?

分類Dev

How to make a recursive fib-function return the correct value with memoization

分類Dev

Why does it not print a list of tuples with no repeated tuples?

分類Dev

Enum of tuples in Swift

分類Dev

Compare Tuple with a List of Tuples

分類Dev

Concatenate tuples in elixir

分類Dev

Read file as a list of tuples

分類Dev

Compare tuples of different sizes

分類Dev

Appending tuple of tuples

分類Dev

Using Tuples as Keys in Python

分類Dev

Unpair list of tuples

分類Dev

How to nest tuples in Python

分類Dev

Python: Filter in tuples

分類Dev

Using tuples with .format in python

分類Dev

Set of Tuples in JavaScript?

分類Dev

Tuples in Scala not working as expected

分類Dev

A monad of tuples (monad, state)?

分類Dev

Set of tuples in Isabelle

分類Dev

Iterating through an array of Tuples

分類Dev

Scala groupby Tuples

分類Dev

What is the need or benefit to use an instance variable/function attribute to implement memoization vs passing down the memo in each call?