Lazy evaluation is an evaluation strategy which holds the evaluation of an expression until its value is needed. It avoids repeated evaluation. Haskell is a good example of such a functional programming language whose fundamentals are based on Lazy Evaluation.
Lazy evaluation is used in Unix map functions to improve their performance by loading only required pages from the disk. No memory will be allocated for the remaining pages.
It allows the language runtime to discard sub-expressions that are not directly linked to the final result of the expression.
It reduces the time complexity of an algorithm by discarding the temporary computations and conditionals.
It allows the programmer to access components of data structures out-of-order after initializing them, as long as they are free from any circular dependencies.
It is best suited for loading data which will be infrequently accessed.
It forces the language runtime to hold the evaluation of sub-expressions until it is required in the final result by creating thunks (delayed objects).
Sometimes it increases space complexity of an algorithm.
It is very difficult to find its performance because it contains thunks of expressions before their execution.
The range method in Python follows the concept of Lazy Evaluation. It saves the execution time for larger ranges and we never require all the values at a time, so it saves memory consumption as well. Take a look at the following example.
r = range(10) print(r) range(0, 10) print(r)
It will produce the following output −
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 3