The functools module in Python deals with higher-order functions, that is, functions operating on(taking as arguments) or returning functions and other such callable objects. The functools module provides a wide array of methods such as
cached_property(func), cmp_to_key(func), lru_cache(func), wraps(func), etc. It is worth noting that these methods take functions as arguments.
lru_cache() is one such function in functools module which helps in reducing the execution time of the function by using memoization technique.
maxsize:This parameter sets the size of the cache, the cache can store upto maxsize most recent function calls, if maxsize is set to None, the LRU feature will be disabled and the cache can grow without any limitations
If typed is set to True, function arguments of different types will be cached separately. For example, f(3) and f(3.0) will be treated as distinct calls with distinct results and they will be stored in two separate entries in the cache
Time taken to execute the function without lru_cache is 0.4448213577270508
Time taken to execute the function with lru_cache is 2.8371810913085938e-05
- Python | functools.wraps() function
- Python Functools - update_wrapper()
- Python Functools - total_ordering()
- Python Functools - cached_property()
- Function Decorators in Python | Set 1 (Introduction)
- Decorators in Python
- Decorators with parameters in Python
- Memoization using decorators in Python
- Class as decorator in python
- Useful cases to illustrate Decorators in python
- call() decorator in Python
- Python Property Decorator - @property
- Multimethods in Python
- Wrapper Class in Python
- Dispatch Decorator in Python
- Create an Exception Logging Decorator in Python
- Implementing LRU Cache Decorator in Python
- Decorator to print Function call details in Python
- Nested Decorators in Python
- Chain Multiple Decorators in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet