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
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
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
Using Iterations in Python Effectively
Prerequisite: Iterators in Python Following are different ways to use iterators. C-style approach: This approach requires prior knowledge of a total number of iterations. Python Code # A C-style way of accessing list elements cars = ["Aston", "Audi", "McLaren"] i = 0 wh
6 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
Python - Run same function in parallel with different parameters
Running the same function in parallel with different parameters involves executing the function multiple times simultaneously, each time with a different set of inputs. For example, if we want to compute the square of each number in the list [0, 1, 2, 3, 4], instead of processing them one by one, ru
5 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
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. C/C++ Code x, y = 10, 20 print(x, y)
2 min read
Convert Generator Object To List in Python
Python, known for its simplicity and versatility, provides developers with a plethora of tools to enhance their coding experience. One such feature is the generator object, which allows for efficient iteration over large datasets without loading them entirely into memory. In this article, we'll expl
3 min read
Python | Timing and Profiling the program
Problems - To find where the program spends its time and make timing measurements. To simply time the whole program, itâs usually easy enough to use something like the Unix time command as shown below. Code #1 : Command to time the whole program bash % time python3 someprogram.py real 0m13.937s user
2 min read
Fast I/O for Competitive Programming in Python
In Competitive Programming, it is important to read input as fast as possible to save valuable time. Input/Output in Python can be sometimes time taking in cases when the input is huge or to output any numbers of lines or a huge number of arrays(lists) line after line. Fast InputNormally, the input
3 min read