Python input methods for concurrent programming



To improve the speed of code execution for intensive I / O tasks, languages ​​have different input and output procedures.

Example task:
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. 
Examples:

 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.

# main input method output
# input N

n = int ( raw_input ())

 
# enter an array

arr = [ int (x) for x in raw_input (). split ()]

 
# initialize variable

summation = 0

 
# calculate the amount

for x in arr:

summation + = x

 
# print response

print (summation)

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.

# import inline stdin

from sys import stdin, stdout 

  
# suppose the function is called main () and
# all operations completed

def main ():

 

  # input via readline method

n = stdin.readline ()

 

# input array similar method

arr = [ int (x) for x in stdin.readline (). split ()]

 

# initialize variable

summation = 0

 

# calculate the amount

for x in arr:

summation + = x

 

# can use inline summation = amount (arr)

 

# print response via write

# write method writes only

# string operations

# so we need to convert any

 

# data into the string for input

stdout.write ( str (summation))

 
# calling the main method

if __ name__ = = "__ main__" :

main () 

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.

# template starts
####################################

 
# importing libraries for handling I / O
# general level

import atexit, io, sys

 
# Implement a stream using bytes in memory
# buffer. It inherits BufferedIOBase.

buffer = io .BytesIO ()

sys.stdout = buffer

 
# print via here

@ atexit . register

def write ():

sys .__ stdout __. write ( buffer . getvalue ())

  
################################# ####
# template ends

 
# normal method followed
  # input N

n = int ( raw_input ())

 
# enter an array

arr = [ int (x) for x in raw_input (). split ()]

 
# initialize the variable

summation = 0

  
# calculate amount

for x in arr:

summation + = x

 
# print response

print (summation)

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.

Note: The above codes are in Python 2.7, for use in Python 3.X versions. Just replace raw_input () with Python syntax 3.X input () . Rest should work fine.

Links:
1. Learn more about Python 2.7 input
2. Output via sys library and other commands.
3. Input via sys library and other commands.
4. Python atexit Document Module.

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.