Writing Memory Efficient Programs Using Generators in Python
Last Updated :
06 May, 2024
When writing code in Python, wise use of memory is important, especially when dealing with large amounts of data. One way to do this is to use Python generators. Generators are like special functions that help save memory by processing data one at a time, rather than all at once.
The logic behind memory-efficient functions and Python generators is to create functions that generate values on the fly, avoiding the need to store the entire data set in memory. This is especially useful when dealing with large data sets.
Memory Efficiency of Generators
Generators in Python are a powerful tool for creating iterators. Let's deep dive into how generators achieve this efficiency and provide a comparison with traditional loops.
- On-the-Fly Sequence Generation: Python generators efficiently create iterators by generating values only when requested, avoiding the need to store the entire sequence in memory simultaneously.
- Memory Efficiency: Unlike traditional data structures, generators use lazy evaluation, producing elements one at a time as needed. This minimizes memory usage, making them ideal for large datasets or infinite sequences.
- Constant Memory Footprint: Generators maintain a consistent memory footprint regardless of sequence size since they don't store the entire sequence in memory. This feature is beneficial for processing large datasets without encountering memory constraints, offering efficient resource utilization.
Code Memory Efficient Functions with Python Generators
Below, are the example of How to Code Memory Efficient Functions with Python Generators.
Basic Generator Function
Start by creating a function with the yield keyword. This turns it into a generator function. Inside, use a loop to generate values one by one. The yield statement provides the current value. This way, the generator produces values on demand, saving memory.
In the below code example, the memory_efficient_function creates numbers from 0 up to the given max_value. The key is that it doesn't keep all the numbers in memory at once. It produces them one by one, which is helpful when you are working with a large set of data and want to save memory.
Python
def memory_efficient_function(max_value):
current_value = 0
while current_value < max_value:
yield current_value
current_value += 1
# Using the generator function
my_generator = memory_efficient_function(5)
for value in my_generator:
print(value)
Real-Life Example with Log File
Consider a scenario where you need to analyze a large log file without loading it all into memory. Create a generator function, like process_log_file, to read the log file line by line. This way, you process the file gradually without storing the whole thing in memory. In this case, the process_log_file function reads the log file line by line and yields each line as it processes it. This way, we will not be loading the entire log file into memory at once. So this way we can make our code more memory-efficient.
Python
def process_log_file(log_file_path):
with open(log_file_path, 'r') as file:
for line in file:
# Process each line of the log file here
yield line
# Use generator to process the log file
log_file_path = '/Path/To/The/file.txt'
log_generator = process_log_file(log_file_path)
for log_entry in log_generator:
# perform actions on each log entry
print(log_entry)
Output
GeeksforGeeks
above code display the output which is written in your file.txt file.
Filtering Data with Generators
Imagine you have a list of numbers, and you only want to work with the even ones. Instead of creating a new list in memory, you can use a generator to produce only the even numbers when needed. This generator function takes a list of numbers as input and produces only the even numbers one at a time. By doing this, you avoid storing a new list of even numbers in memory, making your code more memory-efficient.
Python
def even_number_generator(numbers):
for num in numbers:
if num % 2 == 0:
yield num
# Example usage
numbers_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Create a generator object
even_gen = even_number_generator(numbers_list)
# Iterate through the generator to get even numbers
for even_num in even_gen:
print(even_num)
Generator vs. For Loop
Let's see the memory efficiency of generators by comparing them with traditional For loop using a simple example below:
In this example, we will generate a sequence of numbers from 0 to 999,999 using both a generator and a for loop. We will then compare the memory usage of both approaches using the sys.getsizeof() function.
Python
# Generator function
def generate_numbers(n):
for i in range(n):
yield i
# For Loop Example
def generate_numbers_list(n):
numbers = []
for i in range(n):
numbers.append(i)
return numbers
# comparing memory usage
import sys
n = 1000000 # generating 1 million numbers
# memory usage for Generator
generator_memory = sys.getsizeof(generate_numbers(n))
# memory usage for For Loop
for_loop_memory = sys.getsizeof(generate_numbers_list(n))
print("memory usage for Generator:", generator_memory, "bytes")
print("memory usage for For Loop:", for_loop_memory, "bytes")
Output :
memory usage for Generator: 104 bytes
memory usage for For Loop: 8448728 bytes
Results:
- Memory Usage for Generator: Less memory consumption due to lazy evaluation.
- Memory Usage for For Loop: Higher memory consumption as it stores the entire sequence in memory.
Conclusion
In Conclusion, Python generators are powerful tools for creating memory-efficient functions. They let you handle large amounts of data without loading everything into memory at once. This is crucial for achieving optimal performance and efficiently processing substantial datasets. By using different methods like basic generator functions, real-life log file examples, understanding space complexity, and exploring advanced techniques, you can enhance your code's memory efficiency.
Similar Reads
Using Generators for substantial memory savings in Python
When memory management and maintaining state between the value generated become a tough job for programmers, Python implemented a friendly solution called Generators. Generators With Generators, functions evolve to access and compute data in pieces. Hence functions can return the result to its calle
12 min read
What is the send Function in Python Generators
Python generators are a powerful feature that allows for efficient iteration over potentially large datasets without the need to load everything into memory at once. A generator is a special type of iterable that uses the yield keyword to produce a sequence of values, one at a time. In addition to t
4 min read
Memoization using decorators in Python
Recursion is a programming technique where a function calls itself repeatedly till a termination condition is met. Some of the examples where recursion is used are a calculation of fibonacci series, factorial, etc. But the issue with them is that in the recursion tree, there can be chances that the
3 min read
Python program to print even length words in a string
The task of printing even-length words from a string in Python involves identifying and extracting words whose lengths are divisible by 2. Given an input string, the goal is to filter out words with an even number of characters and display them. For example , s = "Python is great", the even-length w
3 min read
Convert Generator Object To JSON In Python
JSON (JavaScript Object Notation) is a widely used data interchange format, and Python provides excellent support for working with JSON data. However, when it comes to converting generator objects to JSON, there are several methods to consider. In this article, we'll explore some commonly used metho
2 min read
Python | Implementing Dynamic programming using Dictionary
Dynamic Programming is one way which can be used as an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of subproblems so that we do not have to r
3 min read
Output of Python Programs | Set 23 (String in loops)
Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? Python3 my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i ⦠g e e k s f o r g e e k s
2 min read
Get Current Value Of Generator In Python
Python generators are powerful constructs that allow lazy evaluation of data, enabling efficient memory usage and improved performance. When working with generators, it's essential to have tools to inspect their current state. In this article, we'll explore some different methods to get the current
3 min read
Essential Python Tips And Tricks For Programmers
Python is one of the most preferred languages out there. Its brevity and high readability makes it so popular among all programmers. So here are few of the tips and tricks you can use to bring up your Python programming game. 1. In-Place Swapping Of Two Numbers. Python3 x, y = 10, 20 print(x, y) x,
2 min read
Getting Started with Competitive Programming in Python
Python is a great option for programming in Competitive Programming. First off, its easy-to-understand and concise grammar enables quicker development and simpler debugging. The huge standard library of Python offers a wide range of modules and functions that can be used to effectively address progr
11 min read