User Tools

Site Tools


python3:input_output

This is an old revision of the document!


Input and Output

There is no magic bullet that leads to the fastest input and output in a programming contest. Having said this, there are typically multiple ways of performing and processing input and output in Python3. By understanding the options that are available and how to optimize them (e.g., conducting tests) you can see a real world performance increase, in particular when faced with high volumes of reads and writes.

Input Basics

Input in Python3 is handled via the input() function, and reads a newline-terminated string from standard input.1) Additional processing is done to the resulting string.

>>> x = input()
Hello World! # x="Hello World!"
>>> x = int(input())
42 # x=42
>>> x = input().split()
Hello World! # x=["Hello", "World!"]
>>> (x, y) = map(int, input().split())
1 2 # x=1, y=2

map() applies a function across a sequence or iterable structure. The comma , unpacks the map object into x and y.

>>> x = list(map(float, input().split()))
1.2 2.3 3.4 4.5 # x=[1.2, 2.3, 3.4, 4.5]

Output Basics

Output is handled with the print() function.

>>> print() # '\n'
>>> print("Hello World!") # "Hello World!\n"
>>> print("Hello", "World!") # "Hello World!\n"
>>> print(1, 2, 3, sep='') # "123\n"
>>> print(1, 2, 3, end='-')
>>> print("a b c") # "1 2 3-a b c\n"

Advanced Input

input() will scale well for many easy and mid-level contest problems. For mid to upper problems with significant bounds on reads and writes input() will carry an increased risk of time limit exceeded judgements.2)

>>> from sys import *
>>> x = stdin.readline()
Hello World! # x="Hello World!\n"
>>> from sys import *
>>> x = stdin.readline().split()
Hello World! # x=["Hello", "World!"]

readlines() is stopped by an End Of File (EOF) character. You can send this from the terminal with <ctrl-D>

>>> from sys import *
>>> x = stdin.readlines()
Hello
To Everyone! # x=["Hello\n", "To Everyone!\n"]

Benchmarks

The following benchmarks demonstrate the increased likelihood of failure of input() as input sizes increase. All files used for testing can be found here.

10 characters per line (n= number of lines):

n input() sys.stdin.readline() sys.stdin.readlines()
104 .034s .016s .018s
105 .146s .052s .030s
106 1.301s .301s .130s

1000 characters per line (n= number of lines):

n input() sys.stdin.readline() sys.stdin.readlines()
104 .046s .037s .033s
105 .282s .183s .143s
106 2.728s 1.430s 1.723s3)
1)
Unlike C++ and Java, input is line-based rather than token-based.
2)
There is no guaranteed cutoff, but for many problems input() is increasingly likely to fail around 103n≤104 lines.
3)
The readlines() version is actually slower than readline() on the largest dataset. It is attempting to store about 1GB in memory here, causing a slowdown, but still faster than input().
python3/input_output.1534283014.txt.gz · Last modified: 2018/08/14 16:43 by jguerin