简体   繁体   English

Python3中的os.read()和sys.stdin.read()有什么区别

[英]What is the difference between os.read() and sys.stdin.read() in Python3

In the link given below, the answers suggests that both os.read() / os.write() and sys.stdin.read() / sys.stdout.write() can be used for fast I/O. 在下面给出的链接中,答案表明os.read() / os.write()sys.stdin.read() / sys.stdout.write()均可用于快速I / O。 But I didn't find any explanation regarding which among the two is faster or is there any specific case where one performs better than other. 但是我没有找到关于两者中哪一个更快的任何解释,或者在任何特定情况下一个人的表现要好于其他。

Can someone please explain the difference between these methods? 有人可以解释一下这些方法之间的区别吗?

Fastest stdin/out IO in python 3? Python 3中最快的stdin / out IO?

You should measure to check, but sys.stdin and sys.stdout are io.TextIOWrapper objects that, at a minimum, include the additional functionality of decoding incoming data to unicode, and encoding outgoing data to whatever encoding is configured for the output stream. 您应该进行检查,但是sys.stdinsys.stdoutio.TextIOWrapper对象, io.TextIOWrapper对象至少包括将传入数据解码为unicode以及将传出数据编码为为输出流配置的任何编码的附加功能。

On that basis it should be the case that os.read()/os.write() will be faster as these are lower level functions that deal only with bytes, regardless of encoding, if you are dealing with byte oriented data eg ascii text, binary etc. 在此基础上,应该是os.read()/os.write()会更快的情况,因为如果您正在处理面向字节的数据(例如ascii文本os.read()/os.write() ,这些是仅处理字节而与编码无关的较低级函数,二进制等。

If you are profiling the os functions you should also take into account the time to separately encode and decode the data if that is required by your application. 如果要分析os函数,则还应考虑到时间(如果应用程序需要)分别对数据进行编码和解码。 You might find that performing encoding and I/O in two separate steps will be slower than the sys methods. 您可能会发现,在两个单独的步骤中执行编码和I / O将比sys方法慢。

Some other differences in Python 3 are that the os functions accept/return bytes objects whereas the sys objects deal with str objects. Python 3中的其他一些区别是os函数接受/返回bytes对象,而sys对象处理str对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM