📜  使用Python进行竞争性编程的快速I / O

📅  最后修改于: 2021-06-26 02:10:03             🧑  作者: Mango

竞争编程中,重要的是尽快读取输入以节省宝贵的时间。如果输入量巨大或要逐行输出任意行或大量数组(列表), Python的输入/输出有时可能会花费一些时间。

快速输入

通常,使用input()以字符串形式从STDIN中获取输入。法官档案中提供了此STDIN 。因此,请尝试使用操作系统(os)模块和输入/输出(io)模块直接从法官的文件中读取输入。该读取可以以字节的形式完成。通过使用此方法,整数输入可以正常工作,但是对于字符串输入,它将像对象一样将字符串存储为字节。为了更正此问题,可以使用解码函数对字符串进行解码。

以下是Python快速I / O的实现:

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()


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()


输出:

快速输出

除了输出到STDOUT之外,我们还可以尝试写入Judge的系统文件。这样做的代码是在Python使用sys.stdout.write()而不是print()。但是请记住,我们只能使用此方法输出字符串,因此请使用str()或map()将输出转换为字符串。

以下是快速输出的实现:

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()

输出: