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 16:12]
kericson [Advanced Output] - first pass at advanced 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:
 ====== 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 27: Line 31:
  
 ===== Advanced Output ===== ===== Advanced Output =====
- 
-''print()'' is a wrapper around Python's ''write()'' In some instances, ''write()'' may outperform ''print()''. 
- 
-''write()'' requires a single ''str'' argument.  ''write()'' does not perform concatenation, and does //not// automatically insert a ''\n'' at the end of your output. ''write()'' returns the number of characters written to the underlying stream.  This is hidden in competition environments, but it will appear if testing in the [[competitive_programming:python_interpreter|interactive interpreter]]. 
  
 <code python> <code python>
 >>> from sys import * >>> from sys import *
->>> stdout.write('\n'# '\n' +>>> stdout.write('\n')
-</code>+
  
-<code python> +1
->>> from sys import * +
->>> stdout.write("Hello World!\n") # "Hello World!\n"+
 </code> </code>
  
-Concatenation is possible, but needs to be done with the ''+'' operator.+''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> <code python>
 >>> from sys import * >>> from sys import *
->>> stdout.write("Hello" + " " + "World!" + "\n") # "Hello World!\n" +>>> stdout.write("Hello World!\n") # "Hello World!\n" 
-</code> +Hello World! 
- +13
-Numbers can be written using ''write()'', but they need to be cast to string (''str()'') and then concatenated with ''+''+
- +
-<code python> +
->>> from sys import * +
->>> x = 12 +
->>> stdout.write("x is: " + str(x) + "\n") # "x is: 12\n"+
 </code> </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 ====
python3/output.1534367548.txt.gz · Last modified: 2018/08/15 16:12 by kericson