Reading input and output

This is for Python.

When using python and reading files, I am using a different method than what the guide says:
My method for paint:

with open("") as f:
    lines = f.readlines()

#my algorithm(code)

ans = open("paint.out","w")

But the file reading the guide uses is:

import sys

sys.stdin = open("", "r")
sys.stdout = open("paint.out", "w")

#guide algorithm(code)

for i in range(len(cover)):
	ans += cover[i]

Is one of these methods for reading files faster than another or are they the same speed?

1 Like

When there is a lot of input the first method will be faster, because input() is very slow. See this module: Fast Input & Output (this section is about standard I/O, but you can observe a similar difference in speed when using file I/O).

It is probably faster to call readlines() once rather than readline() repeatedly (which the module solution does), though for the problem described in the module, I didn’t observe much of a difference between the two.