Python Functools - lru_cache() Last Updated : 26 Jun, 2020 Comments Improve Suggest changes Like Article 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 Comment More infoAdvertise with us Next Article Company Preparation S sathvik chiramana Follow Improve Article Tags : Python Python Decorators Python functools-module Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like