Python is a very friendly language,The only downside is the slow speed.與 C、C++ 和 Java 相比,它要慢得多.Online coding platform,如果提供的 C/C++ 限制為X.通常,在 Java The time provided in is2X和 Python,它是5X.
To improve input/Code execution speed for output-intensive problems,Languages have various input and output processes.\
示例問題:
Consider looking for user input 的Nthe sum of numbers.
輸入一個數字N.
輸入的Nnumbers are separated by a single space on a line. \
例子:
輸入 :
5
1 2 3 4 5
輸出 :
15
different from the above problems Python 解決方案:
普通方法 Python:(Python 2.7)
1. raw_input() Takes an optional prompt parameter.It also strips trailing newlines from the string it returns.
2. printJust a thin wrapper,It formats the input(args and the trailing newline)並調用給定對象的 write 函數.
# Basic method of input and output
# 輸入 N
n = int(input())
# 輸入數組
arr = [int(x) for x in input().split()]
# 初始化變量
summation = 0
# 計算總和
for x in arr:
summation += x
# 輸出答案
print(summation)
Faster method using built-in standard input,標准輸出:(Python 2.7)
1.另一方面, sys.stdin是File Object.This is just like creating any other file object that can be created to read input from a file.在這種情況下,file will be the standard input buffer.
2. stdout.write('D\n') 比print 'D' 快.
3. Faster is throughstdout.write(“”.join(list-comprehension))
寫入一次,But this makes memory usage dependent on the size of the input.
# Import built-in standard input and output
from sys import stdin, stdout
# 假設有一個名為 main() 的函數,and all operations have been performed
def main():
# 通過 readline 方法輸入
n = stdin.readline()
# Array input is similar
arr = [int(x) for x in stdin.readline().split()]
#初始化變量
summation = 0
# 計算總和
for x in arr:
summation += x
# The built-in summation can be used = sum(arr)
# 通過 write 打印答案
# write The method only writes string operations
# So we need to convert any data to string as input
stdout.write(str(summation))
# 調用主方法
if __name__ == "__main__":
main()
時間上的區別:
Timing summary (100k lines each) ——————————– Print : 6.040 s Write to file : 0.122 s Print with Stdout : 0.121 s正如我們到目前為止所看到的,Taking input from a standard system and giving output to the standard system is always a good idea to improve code efficiency,This is always required for competitive programming.可是等等!Would you like to write these long lines every time you need them?那麼,使用 Python 有什麼好處.
Let's discuss a solution to this problem.What we can do is let us create separate functions to take various types of input,並在需要時調用它們. \
When you want to enter a specific integer integer given in a single line
Suppose the input is of the following form \
5 7 19 20
We want separate variables to refer to them.我們想要的是: \
a = 5
b = 7
c = 19
d = 20
因此,我們可以創建一個名為get_ints() 的函數,如下所示:
import sys
def get_ints(): return map(int, sys.stdin.readline().strip().split())
a,b,c,d = get_ints()
Now you don't have to write this line again and again.您只需調用get_ints() Functions can accept input in this form.在get_ints函數中,我們使用了map 函數.
When you want to enter a list of integers given in a single line
Suppose the input is of the following form\
1 2 3 4 5 6 7 8
We want a single variable to hold the entire list of integers.我們想要的是: \
Arr = [1, 2, 3, 4, 5, 6, 7, 8]
因此,Here we will create a file called get_list() 的函數,如下所示:
import sys
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
Arr = get_ints()
Now you don't have to write this line again and again.您只需調用get_ints() Functions can take input in this form\
when you want to enter a string
Suppose the input is of the following form \
juejin is the best platform to practice coding.
And we hope that a single reference variable will hold this string.我們想要的是: \
string = "juejin is the best platform to practice coding."
因此,Here we will create a file called get_string() 的函數,如下所示:
import sys
def get_string(): return sys.stdin.readline().strip()
string = get_string()
Now you don't have to write this line again and again.您只需要調用get_string() function to get the input in this form Add buffer pipes io:(Python 2.7)
1.Just before submitting the code 添加緩沖 IOcode for faster output.
2.io.BytesIOThe nice thing about objects is that they implement a common interface(通常稱為“類文件”對象).BytesIOObjects have an internal pointer,每次調用 read(n) The pointer will advance.
3.atexitModules provide a simple interface to register functions to be called when the program shuts down gracefully.系統_****Modules also provide a hook sys.exitfunc,But only one function can be registered there.atexit 注冊表Can be used by multiple modules and libraries simultaneously .
# 模板開始
#####################################
# For generic level input/Import library for output processing
import atexit, io, sys
# Stream implementation using in-memory byte buffers. 它繼承了 BufferedIOBase.
buffer = io.BytesIO()
sys.stdout = buffer
# Print via here
@atexit.register
def write():
sys.stdout.write(buffer.getvalue())
#####################################
# 模板結束
# follow the usual approach
# 輸入 N
n = int(input())
# 輸入數組
arr = [int(x) for x in input().split()]
# 初始化變量
summation = 0
# 計算總和
for x in arr:
summation += x
# 打印答案
print(summation)
Usually when dealing with large amounts of data,Normal methods cannot be executed within the time limit.方法 2 Helps maintain a lot I/O 數據.方法 3 是最快的.通常,通過方法 2 和 3 Can help with greater than 2 或 3 MB input data file.
注意: 上述代碼在 Python 2.7 中,用於 Python 3.X 版本.只需將raw_input() 替換為Python 3.X 的 input()語法.Rest should work fine.