Memory Efficiency Comparison: Yield vs. Generator
In this example, below code compares memory usage between a list-based approach (without yield
) and a generator-based approach (with yield
) for generating a sequence of squares up to a specified limit. The generator-based approach demonstrates superior memory efficiency, especially for large limits, as it generates values on-the-fly without storing the entire sequence in memory.
# Without Yield (List-Based Approach)
import sys
def generate_squares_list(limit):
squares = [num ** 2 for num in range(limit)]
return squares
# With Yield (Generator-Based Approach)
def generate_squares_yield(limit):
for num in range(limit):
yield num ** 2
# Memory Comparison
limit = 10**6 # Set a large limit for demonstration purposes
# Without Yield (List-Based Approach)
squares_list = generate_squares_list(limit)
memory_usage_list = sys.getsizeof(squares_list)
# With Yield (Generator-Based Approach)
squares_yield = generate_squares_yield(limit)
memory_usage_yield = sys.getsizeof(squares_yield)
print(f"Memory usage without yield: {memory_usage_list} bytes")
print(f"Memory usage with yield: {memory_usage_yield} bytes")
Output
Memory usage without yield: 8697472 bytes Memory usage with yield: 128 bytes
Conclusion
In conclusion, leveraging generators in Python is a powerful strategy for achieving memory-efficient code, particularly in situations where handling large datasets or dynamic data streams is essential. The lazy evaluation approach of generators allows for on-the-fly generation of values, reducing the overall memory footprint and enabling the processing of data in a scalable and efficient manner.
How to Use Yield Keyword for Memory Efficient Python Code
We are given a task to understand how to use the yield keyword for memory-efficient Python code with the help of different examples with their code. In this article, we will see how to use the yield keyword for memory-efficient Python code.
What is the Yield Keyword?
The yield
keyword in Python is used in the context of generator functions and is fundamental to creating iterators efficiently. It allows a function to pause its execution and yield a value to the caller without losing its state. When the function is called again, it resumes execution from where it left off.