Open In App

Fast I/O for Competitive Programming in Python

Last Updated : 03 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

In Competitive Programming, it is important to read input as fast as possible to save valuable time. Input/Output in Python can be sometimes time taking in cases when the input is huge or to output any numbers of lines or a huge number of arrays(lists) line after line.

Fast Input

Normally, the input is taken from STDIN in the form of String using input(). And this STDIN is provided in the Judge’s file. So try reading the input directly from the Judge’s file using the Operating system(os) module, and input/output (io) module. This reading can be done in the form of bytes. By using this method, integer input works normally, but for string input, it will store the string as a byte like an object. For correcting this, the string can be decoded using the decode function.

Below is the implementation for Fast I/O in Python:

Python3




# Python program to illustrate the use
# of fast Input / Output
import io, os, time
  
# Function to take normal input
def normal_io():
    
      # Stores the start time
    start = time.perf_counter()
      
    # Take Input
    s = input().strip();
      
      # Stores the end time
    end = time.perf_counter()
      
    # Print the time taken
    print("\nTime taken in Normal I / O:", \
                      end - start)
  
# Function for Fast Input
def fast_io():
    
    # Reinitialize the Input function
    # to take input from the Byte Like 
    # objects
    input = io.BytesIO(os.read(0, \
         os.fstat(0).st_size)).readline
  
    # Fast Input / Output
    start = time.perf_counter()
  
    # Taking input as string 
    s = input().decode()
      
      # Stores the end time
    end = time.perf_counter()
  
    # Print the time taken
    print("\nTime taken in Fast I / O:", \
                      end - start)
  
# Driver Code
if __name__ == "__main__":
  
    # Function Call
    normal_io()
      
    fast_io()


Output:

Fast Output

Instead of outputting to the STDOUT, we can try writing to the Judge’s system file. The code for that would be to use sys.stdout.write() instead of print() in Python. But remember we can only output strings using this, so convert the output to a string using str() or map().

Below is the implementation for the Fast Output:

Python3




# Python program to illustrate the use
# of fast Input / Output
import time, sys
  
# Function to take normal input
def normal_out():
    
      # Stores the start time
    start = time.perf_counter()
  
    # Output Integer
    n = 5
    print(n)
  
    # Output String
    s = "GeeksforGeeks"
    print(s)
  
    # Output List
    arr = [1, 2, 3, 4]
    print(*arr)
  
      # Stores the end time
    end = time.perf_counter()
      
    # Print the time taken
    print("\nTime taken in Normal Output:", \
                      end - start)
  
# Function for Fast Output
def fast_out():
  
    start = time.perf_counter()
    # Output Integer
    n = 5
    sys.stdout.write(str(n)+"\n")
  
    # Output String
    s = "GeeksforGeeks\n"
    sys.stdout.write(s)
  
    # Output Array
    arr = [1, 2, 3, 4]
    sys.stdout.write(
        " ".join(map(str, arr)) + "\n"
    )
          
    # Stores the end time
    end = time.perf_counter()
      
    # Print the time taken
    print("\nTime taken in Fast Output:", \
                      end - start)
  
# Driver Code
if __name__ == "__main__":
  
    # Function Call
    normal_out()
      
    fast_out()


Output:



Previous Article
Next Article

Similar Reads

How to Print Fast Output in Competitive Programming using Java?
In Competitive programming, most of the students use C++ as their primary language as it is faster than the other languages(e.g Java, Python) but for a student/professional who use Java as his/her primary language taking Input from input streams and printing fast output is the main difficulty faced during contests on competitive platforms(eg. CodeC
2 min read
Java Competitive Programming Setup in VS Code with Fast I/O and Snippets
Though C++ is the dominating language in the competitive programming universe, there is a fair share of users who still continue to use Java as it has been there seen in the development arena and at the same time can be used competitive programming being fast as it can be toggled to and from where python being slowest among dynamic is hardly seen i
8 min read
Setting up Sublime Text For Competitive Programming (C++) Using Fast Olympic Coding Plugin
Competitive Programming is a mind sport where time matters the most. For that, a fast and easy-to-use tool is required for writing code. Sublime text along with the CppFastOlympicCoding plugin provides the same. Sublime text is popular for its fast performance and aesthetic UI. The CppFastOlympicCoding plugin offers features such as: TestManager: W
4 min read
Fast I/O for Competitive Programming
In competitive programming, it is important to read input as fast as possible so we save valuable time. You must have seen various problem statements saying: "Warning: Large I/O data, be careful with certain languages (though most should be OK if the algorithm is well designed)". The key for such problems is to use Faster I/O techniques. It is ofte
4 min read
Fast I/O in Java in Competitive Programming
Using Java in competitive programming is not something many people would suggest just because of its slow input and output, and well indeed it is slow. In this article, we have discussed some ways to get around the difficulty and change the verdict from TLE to (in most cases) AC. Example: Input: 7 3 1 51 966369 7 9 999996 11 Output: 41. Scanner Cla
7 min read
Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
This is a question asked quite often as in which language should be preferred to be efficient in competitive programming. It is something one should not worry about as what matters is the logic, not the language. Most of the languages are more or less same, but till now the most preferred language is C++, and here are the reasons. Python Simple and
5 min read
Competitive Programming vs General Programming
Programming enthusiasts face a crossroads - Competitive Programming or General Programming? This article sheds light on the distinctions between Competitive Programming vs General Programming, offering a quick guide for those navigating the coding landscape. Whether you're aiming for speed and precision in competitions or tackling real-world projec
3 min read
Dynamic Programming in Game Theory for Competitive Programming
In the fast-paced world of competitive programming, mastering dynamic programming in game theory is the key to solving complex strategic challenges. This article explores how dynamic programming in game theory can enhance your problem-solving skills and strategic insights, giving you a competitive edge. Whether you're a seasoned coder or a newcomer
15+ min read
Top Programming Languages For Competitive Programming
Building an application, running a server, or even implementing a game needs a programming language as the foundation. There are almost more than 700 programming languages which are the most popular ones and this number will increase day by day. But, you don't need to learn all of them. Having a good command of anyone is enough for you to grow your
13 min read
Mathematics Tricks For Competitive Programming In Python 3
Here are some of the exciting features that Python 3.8 provides for programmers in competitive programming. These new features are related to some math functions and algorithms that are frequently used by competitive programmers. The implementation of these features has the time complexity which is the same in case of a program when implemented fro
3 min read
three90RightbarBannerImg