Optimized I/O Operations in Python
Last Updated :
12 Apr, 2024
Python is widely used for file input/output (I/O) operations due to its simplicity and versatility. However, when working with large files or numerous Input/Output operations, optimizing file I/O becomes vital for efficient performance. In this article, we’ll understand how to Optimize I/O operations in Python.
- Pickle Module: The Pickle module in Python provides a way to serialize and deserialize Python objects efficiently and makes it useful for file I/O operations.
- Textual Data I/O: Python provides different built-in functions and methods for reading and writing textual data to files, such as open(), read(), write(), and close(), etc.
Optimized I/O Operations in Python
Below are some of the examples by which we can understand Optimized I/O operations in Python:
Example 1: Using Pickle Module
In this example, the below code serializes a Python dictionary into a binary file using the Pickle module and measures the time taken for both serialization and deserialization. The dictionary is then deserialized back into a Python object, and the serialized data along with the times are printed.
Python3
import pickle
import time
# Sample data to be serialized
data = {'name': 'Nibedita', 'age': 20, 'Profession': 'Python Programmer'}
# Serialization
start_time = time.time()
with open('data.pkl', 'wb') as file:
pickle.dump(data, file)
serialization_time = time.time() - start_time
# Deserialization
start_time = time.time()
with open('data.pkl', 'rb') as file:
loaded_data = pickle.load(file)
deserialization_time = time.time() - start_time
print("Serialized data:", loaded_data)
print("Serialization time:", serialization_time, "seconds")
print("Deserialization time:", deserialization_time, "seconds")
Output:
Serialized data: {'name': 'Nibedita', 'age': 20, 'Profession': 'Python Programmer'}
Serialization time: 0.001481771469116211 seconds
Deserialization time: 0.0002384185791015625 seconds
Example 2: Efficient Textual Data I/O Operations
In this example, below code measures the time taken for writing one million lines to a text file and subsequently reading them. It using a For loop to write each line and iterates through the file to read the lines. Finally, it prints the times taken for writing and reading operations.
Python3
import time
# Writing to a file
start_time = time.time()
with open('text_data.txt', 'w') as file:
for i in range(1000000):
file.write(f"Line {i}\n")
writing_time = time.time() - start_time
# Reading from a file
start_time = time.time()
with open('text_data.txt', 'r') as file:
for line in file:
pass
reading_time = time.time() - start_time
print("Writing time:", writing_time, "seconds")
print("Reading time:", reading_time, "seconds")
Output:
Writing time: 0.3550088405609131 seconds
Reading time: 0.08370423316955566 seconds
Share your thoughts in the comments
Please Login to comment...