[Python] Speeding up processing using cache tools

--Investigate whether python can also speed up processing using cache. --As a result, it turned out that it can be easily used by using a library called cachetools. ――Therefore, this time, the outline of the library and the description example of the process are described. -** * Since this content deals with the outline, result image, and basic description, refer to Document for details. ** **

Overview

--Cachetools is a collection library that summarizes high-speed processing (memoization) using cache. --The features are as follows. --You can handle many cache algorithms with few descriptions. --It is expandable and can be changed to an appropriate mechanism according to the application and environment. --Data is stored in RAM.

result

――As shown in the two comparison images below, it is possible to easily speed up heavy processing with a small amount of description.

image.png

image.png

environment

Installation

--Install the cachetools library with the following command.

pip install cachetools

Description example

** * Since this content deals with the outline, result image, and basic description, refer to Document for details. ** **

Basic

--When using standard cache processing (no options specified).

#Loading the library
from cachetools import cached

#Cache activation
@cached(cache ={})
def fibonacci(n):
  if n <= 2:
    return 1
  else:
    return fibonacci(n - 2) + fibonacci(n - 1)

print(fibonacci(30))

Each option

--The following types of cache processing can be used.

name Contents
TTLCache Specify the cache lifetime time.
No access to anything that exceeds the lifetime.
Discard the least used items first.
LFUCache Minimum frequency of use. Measure item acquisition frequency and discard the least frequently used items
LRUCache Maximum unused frequency. Discard from the least used one
RRCache random. Randomly select items and discard from there

--The following is a description example

#Loading the library
from cachetools import cached, LFUCache, TTLCache, RRCache

@cached(cache=LFUCache(maxsize=10))  #Maximum number of holdings
def get_all_item:
  #processing

@cached(cache=TTLCache(maxsize=10, ttl=300))  #Maximum retention and lifetime
def get_user_item:
  #processing

@cached(cache=RRCache(maxsize=10, choice=min))  #Alternate function that returns the maximum number of holdings and any element
def get_random:
  #processing. cache.Function specified by choice(min)Can be called.

reference

Recommended Posts

[Python] Speeding up processing using cache tools
Regarding speeding up python (memo)
Using Python mode in Processing
Periodic execution processing when using tkinter [Python3]
[Python] Matrix multiplication processing time using NumPy
Experience Linux speeding up with Page cache
Setting up Basic authentication using Python @Lambda
[Python] Various data processing using Numpy arrays
Video processing using Python + OpenCV on Mac
Setting up Digest authentication using Python @Lambda
VBA user tried using Python / R: Iterative processing
python image processing
Speeding up numerical calculation using NumPy / SciPy: Application 2
Start using Python
Python file processing
[Competition Pro] Speeding up DP using cumulative sum
Speeding up numerical calculation using NumPy / SciPy: Application 1
Scraping using Python
How to set up a Python environment using pyenv
Tips for speeding up python code correctly with numba
Process csv data with python (count processing using pandas)
A note on speeding up Python code with Numba
Reading, displaying and speeding up gifs with python [OpenCV]
Python distributed processing Spartan
Operate Redmine using Python Redmine
File processing in Python
Python: Natural language processing
Communication processing by Python
Fibonacci sequence using Python
Multithreaded processing in python
Dictionary-type processing using items ()
Data analysis using Python 0
First Python image processing
Various Python visualization tools
Data cleaning using Python
Text processing in Python
Queue processing in Python
Refactoring tools for Python
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Image processing with Python
Search Twitter using Python
Name identification using python
Notes using Python subprocesses
Try using Tweepy [Python2.7]
Python string processing illustration
Various processing of Python
Speeding up numerical calculation using NumPy / SciPy: Picking up fallen ears
Wrap C/C ++ with SWIG to speed up Python processing. [Overview]
Asynchronous processing using LINE BOT: RQ (Redis Queue) in Python