whi*_*ile 14 python io performance stdin python-3.x
我一直在使用python 3.1.2在SPOJ.pl上解决一些问题,而一些人快速导致简单的问题让我想知道是否有更快的方法来处理输入和输出.
我试过用了
input()
print()
Run Code Online (Sandbox Code Playgroud)
和
sys.stdin.readline()
sys.stdout.write()
Run Code Online (Sandbox Code Playgroud)
更确切地说
for line in sys.stdin:
#Handle input
sys.stdout.write(output)
Run Code Online (Sandbox Code Playgroud)
处理每一行.我还尝试收集列表中的所有输出,并在处理完所有内容时立即打印.
但所有这些都会产生类似的执行时间.
有没有更快的方法来处理stdin/out的输入和输出?
Sve*_*ach 11
以下可能是最快的:
使用即时读取所有输入os.read(0, some_big_enough_number).
处理输出,将结果收集到列表中results.
使用一次写入所有输出os.write(1, "".join(results)).
我记得有一个案例我注意到了os.read(),os.write()有时比使用Python I/O更快,但我不记得细节了.