擅长:python、mysql、java
<p>通过使用lru_缓存,可以使函数在需要时间的python代码中变得高效,它存储缓存并减少运行时间</p>
<p>语法-</p>
<pre><code># importing module
from functools import lru_cache
# we use lru_cache as a decorator , it takes maxsize argument
@lru_cache(maxsize=10)
def exampleFunction(str):
time.sleep(2s)
print(str)
# exampleFunction will take 2 seconds here to run -
exampleFunction ("when function called first time i took 2 seconds to run")
# now exampleFunction will not take a second to run as it is stored in cache
exampleFunction("exampleFunction didn't take a single second now to run")
</code></pre>