如何“缓存”耗时的结果?

2024-04-19 05:33:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我经常遇到这样的情况,我有数据,这是耗时的获取。这在调试时变得特别烦人。你知道吗

我通常首先运行查询并将结果转储到pickle中:

import pickle
d = the_data_query_which_takes_time()
with open("cache.pickle", "wb") as f:
    pickle.dump(d, f)

然后,对于调试/测试:

import pickle
#d = the_data_query_which_takes_time()
with open("cache.pickle", "rb") as f:
    d = pickle.load(f)

虽然这基本上是可行的,但它不是一种非常实用的缓存结果的方法。有没有一种更适合Python和可重复使用的方法?你知道吗


Tags: the数据方法importcachewhichdatatime
1条回答
网友
1楼 · 发布于 2024-04-19 05:33:51

我想你在找一种叫做memoization的东西:

The term "memoization" was introduced by Donald Michie in the year 1968. It's based on the Latin word memorandum, meaning "to be remembered". It's not a misspelling of the word memorization, though in a way it has something in common. Memoisation is a technique used in computing to speed up programs. This is accomplished by memorizing the calculation results of processed input such as the results of function calls. If the same input or a function call with the same parameters is used, the previously stored results can be used again and unnecessary calculation are avoided. In many cases a simple array is used for storing the results, but lots of other structures can be used as well, such as associative arrays, called hashes in Perl or dictionaries in Python.

Memoization can be explicitly programmed by the programmer, but some programming languages like Python provide mechanisms to automatically memoize functions.

从Pythonic的角度来看,这通常是通过decorator或类来完成的。下面是一个涉及装饰师的简单案例:

def memoize(func):
    S = {}
    def wrappingfunction(*args):
        if args not in S:
            S[args] = func(*args)
        return S[args]
    return wrappingfunction

# This function is now memoized
@memoize
def cube(x):
    return x**3

以下是一些有用的链接,可帮助您入门:

http://www.python-course.eu/python3_memoization.php

https://wiki.python.org/moin/PythonDecoratorLibrary

http://www.thumbtack.com/engineering/a-primer-on-python-decorators/

http://www.pycogsci.info/?p=221

相关问题 更多 >