Python function buffer

Python cache###

#1 surroundings##

Python3.7.3 # Python>=3.2

#2 Start##

#2.1 What is a cache###

I usually hear that redis is used for caching, but redis changes the cache to store the result data. Starting from version 3.2 of Python, a very elegant caching machine has been introduced

from functools import lru_cache

lru_cache can improve the efficiency of program execution, especially suitable for time-consuming functions. You only need to add decorators to the required functions to achieve the effect of caching, especially some recursive functions

# 2.2 Test (Fibonacci Sequence)

  1. No cache used
def fab(n):if n <=2:return n

 returnfab(n-1)+fab(n-2)print(datetime.datetime.now()) # 2019-05-2414:21:43fab(40)print(datetime.datetime.now()) # 2019-05-2414:22:20

When the cache is not used, the running time of fab(40) takes about 37 seconds??

  1. Plus cache
from functools import lru_cache
import datetime

@ lru_cache(None)
def fab(n):if n <=2:return n

 returnfab(n-1)+fab(n-2)print(datetime.datetime.now()) # 2019-05-2414:24:00.229715fab(40)print(datetime.datetime.now()) # 2019-05-2414:24:00.229823

When the cache is added, it takes less than 1 second to execute fab(40)??

#2.3 Why is it like this

Do a little test

  1. No cache used
def fab(n):print(n)return None

fab(10)fab(10)fab(10)


  1. Use cache
from functools import lru_cache

@ lru_cache(None)
def fab(n):print(n)return None

fab(10)fab(10)fab(10)fab(9)fab(9)


**It can be seen from the result that when fab(10) is called the second time, the function body is not actually executed, but the cached result is directly returned. **

#2.4 lur_cache parameters/methods###

**Using the lur_cache decorator of the functools module can cache up to maxsize call results of this function, thereby improving the efficiency of program execution, especially suitable for time-consuming functions. The parameter maxsize is the maximum number of caches. If it is None, there is no limit. When set to 2n, the performance is the best; if typed=True (note that there is no such parameter in functools32), calls of different parameter types will be cached separately. For example, f(3) and f(3.0). **** The function decorated by lru_cache has two methods, cache_clear and cache_info, which are used to clear the cache and view cache information respectively. **

Recommended Posts

Python function buffer
Python enumerate() function
Python custom function basics
Join function in Python
Python function basic learning
Python data analysis-apply function
Python3 built-in function table.md
Python Print print timer function
Python defines a function method
Python high-order function usage summary!
Python realizes online translation function
Python tornado upload file function
How Python implements FTP function
Python implements image stitching function
Python high-order function usage summary!
Python telnet login function implementation code
Python| function using recursion to solve
How Python implements the mail function
Why doesn&#39;t Python support function overloading?
Is there function overloading in python
Python enumerate() function
Python function buffer
Learning Python third day one-line function
Python function definition and parameter explanation
Python ATM function implementation code example
Python implements AI face change function
Python realizes image recognition car function
Python implements ftp file transfer function
Python realizes udp transmission picture function
How Python implements the timer function
Python multithreading
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
How to run id function in python
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
What is the id function of python
Python toolchain
What is an anonymous function in Python
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
How does python call its own function
Python basics 3
python3 realizes the function of mask drawing
Python multitasking-threads
Python functions
python sys.stdout
python operator