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 print() sys.stdout.write()
104 .006 .002
105 .059 .022
106 .378 .202
107 7.341 6.325

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

n print() sys.stdout.write()
104 .016 .011
105 3.886 3.652
106

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

1)
We deliberately avoided additional processing such as typecasts, map(), and split(), as these are non-IO considerations in Python3.
python3/output.1534366031.txt.gz · Last modified: 2018/08/15 15:47 by kericson