Python Functools - lru_cache() Last Updated : 26 Jun, 2020 Comments Improve Suggest changes 18 Likes Like Report 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() lru_cache() is one such function in functools module which helps in reducing the execution time of the function by using memoization technique. Syntax: @lru_cache(maxsize=128, typed=False) Parameters: 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 typed: 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 Example:1 Python3 1== from functools import lru_cache import time # Function that computes Fibonacci # numbers without lru_cache def fib_without_cache(n): if n < 2: return n return fib_without_cache(n-1) + fib_without_cache(n-2) # Execution start time begin = time.time() fib_without_cache(30) # Execution end time end = time.time() print("Time taken to execute the\ function without lru_cache is", end-begin) # Function that computes Fibonacci # numbers with lru_cache @lru_cache(maxsize = 128) def fib_with_cache(n): if n < 2: return n return fib_with_cache(n-1) + fib_with_cache(n-2) begin = time.time() fib_with_cache(30) end = time.time() print("Time taken to execute the \ function with lru_cache is", end-begin) Output: 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 Example 2: Python3 from functools import lru_cache @lru_cache(maxsize = 100) def count_vowels(sentence): sentence = sentence.casefold() return sum(sentence.count(vowel) for vowel in 'aeiou') print(count_vowels("Welcome to Geeksforgeeks")) Output: 9 Create Quiz Comment S sathvik chiramana Follow 18 Improve S sathvik chiramana Follow 18 Improve Article Tags : Python Python Decorators Python functools-module Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like