Store past results of a function

I have a function that takes in two arrays, does some calculation, and then returns a new array. The function is called maybe 60,000 times (or 500k, depending), where up to 25% of the times it is called, it is redoing a previous calculation.

prev_calc = {}
def arrayMagic(ar1,ar2):
    ar1_t = tuple(ar1)
    ar2_t = tuple(ar2)

    if (ar1_t,ar2_t) in prev_calc: 
        return prev_calc[(ar1_t,ar2_t)]
    #some somewhat, but not too expensive function
    res = magicCalc(ar1,ar2)

    prev_calc[(ar1_t,ar2_t)] = res

    return res

ar1 and ar2 are arrays of 3 floats each; for example: [1.1,1.2,2.2] res is also an array of 3 floats.

The problem is that the tuple conversion and dictionary lookup happen to be very close to the time the magicCalc() function takes. Since this is the major bottleneck in my code, optimizing this would solve alot. The function is passed the two arrays, so I can't have them as an array already.

Is there a fast way to store past results of a function and return them?

1 answer

  • answered 2020-01-14 02:01 TomG12

    This seems pretty quick...

    d = dict()
    l1 = [1.1,21.1,31.1,41.1,51.1]
    l2 = [12.1,21.1,31.1,41.1,51.1]
    
    
    def do(a1):
        cacheKey = str(a1) 
        if cacheKey in d:
            print("Read from cache")
            return d[cacheKey]
        else:
            print("calc and cache result")
            d[cacheKey] = sum(a1)
            return d[cacheKey]
    
    print(do(l1))
    print(do(l2))
    print(do(l1))