БГ
Size: a a a
БГ
<Ч
БГ
<Ч
B
B
B
VA
VA
from functools import lru_cache
@lru_cache(maxsize=32)
def say(phrase):
print(phrase)
return len(phrase)
say('hello')
# hello
# 5
say('pythonetc')
# pythonetc
# 9
# the function is not called, the result is cached
say('hello')
# 5
say({})
# TypeError: unhashable type: 'dict'
@lru_cache(maxsize=32)
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
fib(30)
# 832040
fib.cache_info()
# CacheInfo(hits=27, misses=30, maxsize=32, currsize=30)
fib.cache_clear()
fib.cache_info()
# CacheInfo(hits=0, misses=0, maxsize=32, currsize=0)
# Introduced in Python 3.9:
fib.cache_parameters()
# {'maxsize': None, 'typed': False}
lru_cache
is:def nop():
return None
@lru_cache(maxsize=1)
def nop_cached():
return None
%timeit nop()
# 49 ns ± 0.348 ns per loop
# cached faster!
%timeit nop_cached()
# 39.3 ns ± 0.118 ns per loop
БГ
from functools import lru_cache
@lru_cache(maxsize=32)
def say(phrase):
print(phrase)
return len(phrase)
say('hello')
# hello
# 5
say('pythonetc')
# pythonetc
# 9
# the function is not called, the result is cached
say('hello')
# 5
say({})
# TypeError: unhashable type: 'dict'
@lru_cache(maxsize=32)
def fib(n):
if n <= 2:
return 1
return fib(n-1) + fib(n-2)
fib(30)
# 832040
fib.cache_info()
# CacheInfo(hits=27, misses=30, maxsize=32, currsize=30)
fib.cache_clear()
fib.cache_info()
# CacheInfo(hits=0, misses=0, maxsize=32, currsize=0)
# Introduced in Python 3.9:
fib.cache_parameters()
# {'maxsize': None, 'typed': False}
lru_cache
is:def nop():
return None
@lru_cache(maxsize=1)
def nop_cached():
return None
%timeit nop()
# 49 ns ± 0.348 ns per loop
# cached faster!
%timeit nop_cached()
# 39.3 ns ± 0.118 ns per loop
БГ
<Ч
ТВ
I
@
@
AT
AT
КБ