![]() |
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.
Optimized I/O Operations in PythonBelow are some of the examples by which we can understand Optimized I/O operations in Python: Example 1: Using Pickle ModuleIn 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.
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 OperationsIn 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.
Output: Writing time: 0.3550088405609131 seconds
Reading time: 0.08370423316955566 seconds |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |