记忆化导致结果变慢

0 投票
0 回答
111 浏览
提问于 2025-04-11 21:58

我正在创建一个记忆化的例子,主要是写一个函数来计算数组中元素的总和或平均值,并且和之前存储的结果进行比较,以便在结果已经存储的情况下直接取用。

另外,我想要设置一个条件,只有当函数的结果差别很大(比如低于5000)时,才会存储这个结果。

我用一个装饰器来实现这个功能,但使用装饰器的结果比不使用记忆化要慢一点,这让我觉得不太好。另外,装饰器的逻辑是否正确呢?

我的代码在下面:

import time

import random
from collections import OrderedDict

def memoize(f):
    cache = {}
    def g(*args):
        sum_key_arr = sum(args[0]) 
        print(sum_key_arr)
        if sum_key_arr not in cache:
            for key, value in OrderedDict(sorted(cache.items())).items():# key in dict cannot be an array so I use the sum of the array as the key
                if abs(sum_key_arr - key) <= 5000:#threshold is great here so that all values are approximated!
                    #print('approximated')
                    return cache[key]
            else:
                #print('not approximated')
                cache[sum_key_arr] = f(args[0],args[1])
        return cache[sum_key_arr]  
    return g
@memoize
def aggregate(dict_list_arr,operation):
    if operation == 'avg':
        return sum(dict_list_arr) / len(list(dict_list_arr)) 
    if operation == 'sum':
        return sum(dict_list_arr)
    return None
    
t = time.time()
for i in range(200,150000):
    res = aggregate(list(range(i)),'avg')
elapsed = time.time() - t
print(res)
print(elapsed)

更新:我尝试引入一个ID键(用来捕捉列表的内容),现在使用字典作为输入,下面是我对代码所做的更改:

import time
import random
from collections import OrderedDict

def memoize(f):
    cache = {}
    def g(*args):
        
        key_arr = list(args[0].keys())[0]
        if key_arr not in cache:
            for key, value in OrderedDict(sorted(cache.items())).items():# key in dict cannot be an array so I use the sum of the array as the key
                if abs(int(key_arr) - int(key)) <= 5000:#threshold is great here so that all values are approximated!
                    print('approximated')
                    return cache[key]
            else:
                print('not approximated')
                cache[key_arr] = f(args[0])
        return cache[key_arr]  
    return g
#@memoize
def aggregate(dict_list_arr):
     
    #if operation == 'avg':
    return sum(list(dict_list_arr.values())[0]) / len(list(dict_list_arr.values())[0]) 
    # if operation == 'sum':
        # return sum(dict_list_arr)
    # None
    
t = time.time()
for i in range(200,15000):
    res = aggregate({str(1+i):list(range(i))})
elapsed = time.time() - t
print(res)
print(elapsed)

0 个回答

暂无回答

撰写回答