Memoization using Decorators in Python
In this tutorial, we will discuss one of the advance concepts of Python decorator. We assume that, you have basic understanding of the Python decorators. If not, you can learn from of Decorator in Python tutorial.
What is Memoization?
Before learning about the memoization, let's have a brief introduction of Recursion A recursion is a technique where a function calls itself repeatedly till the base case condition is met. For example calculation of Fibonacci series, factorial, etc. But it creates the problem with the recursion tree; there can be chances that the sub-problem that is already solved is being solved again, which leads to be overhead in the memory. To overcome such error, memoization comes into the play.
Memoization is a programming technique which records the intermediate results so that it can be used to overlook the repeated calculations and makes the fast execution of program. This technique is used to optimize the recursion based program with the help of decorators.
Let's understand the following example of calculating the factorial of a number using the recursion.
The same program can be implemented using the memoization decorators.
In the above code
In this tutorial, we have explained the Python advance concept of decorator memoization. It is quite helpful in recursion tree problems. It reduces the memory heads.
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week