To improve the speed of code execution for intensive I / O tasks, languages have different input and output procedures.
Consider the question of finding a sum N numbers entered by the user.
Enter the number N.
Enter the N numbers, separated by one space per line.
Input: 5 1 2 3 4 5 Output: 15
Various Python solutions for the above problem:
Normal Python method: (Python 2.7)
1. raw_input () takes an optional prompt argument. It also removes the trailing newline character from the returned string.
2. print — it`s just a thin wrapper that formats the input (a space between arguments and a newline at the end) and calls the write function on the given object.
Slightly faster method using inline stdin, stdout: (Python 2.7)
1. sys.stdin, with another th side is File Object . This is similar to creating any other file object that you can create to read input from a file. In this case, the file will be the standard input buffer.
2. stdout.write (& # 39; D / n & # 39;) is faster than printing & # 39; D & # 39; .
3. It`s even faster to write everything once with stdout.write ("". Join (list-compceptionsion)), but this makes memory usage dependent on the size of the input.
The difference is time:
Timing summary (100k lines each)
Print: 6.040 s
Write to file: 0.122 s
Print with Stdout: 0.121 s
Add a buffered io pipe: (Python 2.7)
1. Simple add buffered th I / O code before the submit code to speed up the output.
2. The advantage of io.BytesIO objects is that they implement a common interface (commonly referred to as a "file" object). BytesIO objects have an internal pointer, and for each call to read (n), the pointer is advanced.
3. The atexit module provides a simple interface for registering functions that are called when the program is closed normally. The sys module also provides a sys.exitfunc hook, but only one function can be registered there. The atexit registry can be used by multiple modules and libraries at the same time.
Usually, when processing a lot of data, the usual method cannot be completed in a timely manner. Method 2 helps to maintain a large amount of I / O data. Method 3 is the fastest. Typically, input data files larger than 2 or 3 MB are processed using methods 2 and 3.
This article courtesy of Shubham Saxena . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.