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. 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?
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.
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 e.g. ascii text, binary etc.
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. You might find that performing encoding and I/O in two separate steps will be slower than the sys
methods.
Some other differences in Python 3 are that the os
functions accept/return bytes
objects whereas the sys
objects deal with str
objects.