User Tools

Site Tools


python3:output

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
python3:output [2018/08/15 10:57]
jguerin Started removing references to input().
python3:output [2018/08/15 16:33] (current)
jguerin Added byte count output to shell examples, removed and condensed associated descriptions.
Line 1: Line 1:
 ====== Output in Python3 ====== ====== 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. 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.
 +
 +Unlike [[python3:input|Python3 input]], we noted less dramatic changes when the faster ''stdout.write()'' call was used over ''print()''.((It is possible that ''print()'''s implementation is a thin wrapper around something like ''stdout.write()'', in which case the formatting and convenience of ''print()'' account for the major differences.)) In instances where you believe very small fractions of a second may get you out of a //time limit exceeded// judgement, switching may be warranted (in particular when the number of writes are excessive).
 +
 +Otherwise, fine tuning of algorithm implementations and input or the selection of C++ may be better choices than fine tuning ''print()''.
  
 ===== Output Basics ===== ===== Output Basics =====
Line 28: Line 32:
 ===== Advanced Output ===== ===== Advanced Output =====
  
 +<code python>
 +>>> from sys import *
 +>>> stdout.write('\n')
 +
 +1
 +</code>
 +
 +''stdout.write()'' returns the number of characters written to the underlying stream, but only prints the information in the context of the [[competitive_programming:python_interpreter|interactive interpreter]].((This information is suppressed when ''stdout.write()'' is used in the context of a ''.py'' file, and hence requires no special handling in contests.))
 +
 +<code python>
 +>>> from sys import *
 +>>> stdout.write("Hello World!\n") # "Hello World!\n"
 +Hello World!
 +13
 +</code>
 +
 +Unlike ''print()'', ''stdout.write()'' takes only a single parameter and offers no formatting options. ''stdout.write()'' over multiple strings requires multiple calls or concatenations. Other types must be converted to string before they are printable.
  
 ==== Benchmarks ==== ==== Benchmarks ====
-The following benchmarks demonstrate the increased likelihood of failure of ''print()'' as output sizes increase. All files used for testing can be found [[files:python3:io_tests|here]].+The following benchmarks demonstrate the increased likelihood of failure of ''print()'' as output sizes increase. All files used for testing can be found [[files:python3:output_tests|here]].
  
 10 characters per line (//n//= number of lines): 10 characters per line (//n//= number of lines):
-| //n//          | input() | sys.stdin.readline() | sys.stdin.readlines() | +| //n//          | print() | sys.stdout.write() | 
-| 10<sup>4</sup> | .034s    | .016s                 | .018s                  +| 10<sup>4</sup> | .006    | .002               |  
-| 10<sup>5</sup> | .146s    | .052s                 | .030s                  +| 10<sup>5</sup> | .059    | .022               
-| 10<sup>6</sup>1.301s   | .301s                 | .130s                  |+| 10<sup>6</sup> | .378    | .202               |
  
  
 1000 characters per line (//n//= number of lines): 1000 characters per line (//n//= number of lines):
-| //n//          | input() | sys.stdin.readline() | sys.stdin.readlines() | +| //n//          | print() | sys.stdout.write() | 
-| 10<sup>4</sup> | .046s    | .037s                 | .033s                  +| 10<sup>4</sup> | .016    | .011               
-| 10<sup>5</sup> | .282s    | .183s                 | .143s                  | +| 10<sup>5</sup>3.886   | 3.652              |
-| 10<sup>6</sup> | 2.728s   | 1.430s                1.723s((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()''+
-))                 | +
- +
-The above tests were designed to showcase minimal reading functionality other than temporary storage.((We deliberately avoided additional processing such as typecasts, ''map()'', and ''split()'', as these are non-IO considerations in Python3.))+
  
 +The above tests were designed to showcase minimal printing functionality on data that is currently residing in memory.
  
  
python3/output.1534348663.txt.gz · Last modified: 2018/08/15 10:57 by jguerin