User Tools

Site Tools


python3:output

This is an old revision of the document!


Output in Python3

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.

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 Output

Benchmarks

The following benchmarks demonstrate the increased likelihood of failure of print() as output 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.723s1)

The above tests were designed to showcase minimal reading functionality other than temporary storage.2)

1)
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().
2)
We deliberately avoided additional processing such as typecasts, map(), and split(), as these are non-IO considerations in Python3.
python3/output.1534348663.txt.gz · Last modified: 2018/08/15 10:57 by jguerin