User Tools

Site Tools


python3:output

Differences

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

Link to this comparison view

Next revision
Previous revision
python3:output [2018/08/15 10:05]
jguerin Moved from input_output.
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:
-====== Input and Output ======+====== 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.
  
-===== Input Basics ===== +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).
-Input in Python3 is handled via the ''%%input()%%'' function, and reads a newline-terminated string from standard input.((Unlike C++ and Java, input is line-based rather than token-based.)) Any additional processing is done to the resulting string.+
  
-<code python> +Otherwisefine tuning of algorithm implementations and input or the selection of C++ may be better choices than fine tuning ''print()''.
->>> x = input() +
-Hello World! # x="Hello World!" +
-</code> +
- +
- +
-<code python> +
->>> x = int(input()) +
-42 # x=42 +
-</code> +
- +
-<code python> +
->>> x = input().split() +
-Hello World! # x=["Hello", "World!"+
-</code> +
- +
-<code python> +
->>> (x, y) = map(int, input().split()) +
-1 2 # x=1, y=2 +
-</code> +
- +
-''[[python3:map_reduce_filter|map()]]'' applies a function across a sequence or iterable structure. The comma '','' [[python3:unpacking | unpacks]] the map object into ''x'' and ''y''+
- +
-<code python> +
->>> x = list(map(float, input().split())) +
-1.2 2.3 3.4 4.5 # x=[1.2, 2.3, 3.4, 4.5] +
-</code>+
  
 ===== Output Basics ===== ===== Output Basics =====
Line 57: Line 30:
 </code> </code>
  
-===== Advanced Input ===== +===== Advanced Output =====
-''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.((There is no guaranteed cutoff, but for many problems ''input()'' is //increasingly likely// to fail around 10<sup>3</sup>≤//n//≤10<sup>4</sup> lines.)) +
- +
-=== stdin.readline() === +
-''readline()'' is identical to the interface provided by ''input()'' except it //retains// the ''\n'' that is used to terminate the read.((This may require special handling in certain circumstance and can be safely ignored in others. E.g., ''.split()'' will //not// behave differently vs. ''input()''.))+
  
 <code python> <code python>
 >>> from sys import * >>> from sys import *
->>> x = stdin.readline(+>>> stdout.write('\n')
-Hello World! # x="Hello World!\n+
-</code>+
  
 +1
 +</code>
  
-=== stdin.readlines() === +''stdout.write()'' returns the number of characters written to the underlying streambut 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.))
-''readlines()'' processes an entire fileterminated by an //end of file// character.((This behavior can be simulated on the terminal with ''<ctrl>+d''.))+
  
 <code python> <code python>
 >>> from sys import * >>> from sys import *
->>> x = stdin.readlines(+>>> stdout.write("Hello World!\n") # "Hello World!\n" 
-Hello +Hello World! 
-To Everyone! # x=["Hello\n""To Everyone!\n"]+13
 </code> </code>
  
-''<ctrl>+d'' would be used to terminate this example after the ''!''. +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.
- +
-''readlines()'' loads //and// stores an entire file into memory (as a list) its performance will exceed other options in Python3 for all but the most enormous files.((See the table below for an example where ''readlines()'' required storing ∼1GB of data.)) +
  
 ==== Benchmarks ==== ==== Benchmarks ====
-The following benchmarks demonstrate the increased likelihood of failure of ''input()'' as input 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.1534345507.txt.gz · Last modified: 2018/08/15 10:05 by jguerin