Search code examples
pythonperformanceinputiooutput

Fast I/O for competative programming in python


I have using standard input() to read the string in the competitive programming but its throws NZEC error. The reason is the huge data set so we have to use I/O optimization. I referred to below link: https://www.geeksforgeeks.org/fast-i-o-for-competitive-programming-in-python/

import io, os
t = int(input())
for k in range(t):
    input = io.BytesIO(os.read(0, \
         os.fstat(0).st_size)).readline

    s = input().decode()
    print(s)

Here it prints empty line without reading string value. Please help me to read I/O in the fastest way.


Solution

  • stdin helps to read input faster

    from sys import stdin
    s = stdin.readline()
    

    It works well to reads the input faster.