title: Why is printing to stdout so slow? Can it be sped up?
body: <p>I've always been amazed/frustrated with how long it takes to simply output to the terminal with a print statement.  After some recent painfully slow logging I decided to look into it and was quite surprised to find that almost <strong>all</strong> the time spent is waiting for the terminal to process the results.</p>
  
  <p>Can writing to stdout be sped up somehow?</p>
  
  <p>I wrote a script ('<code>print_timer.py</code>' at the bottom of this question) to compare timing when writing 100k lines to stdout, to file, and with stdout redirected to <code>/dev/null</code>.  Here is the timing result:</p>
  
  <pre><code>$ python print_timer.py
  this is a test
  this is a test
  &lt;snipped 99997 lines&gt;
  this is a test
  -----
  timing summary (100k lines each)
  -----
  print                         :11.950 s
  write to file (+ fsync)       : 0.122 s
  print with stdout = /dev/null : 0.050 s
  </code></pre>
  
  <p>Wow.  To make sure python isn't doing something behind the scenes like recognizing that I reassigned stdout to /dev/null or something, I did the redirection outside the script...</p>
  
  <pre><code>$ python print_timer.py &gt; /dev/null
  -----
  timing summary (100k lines each)
  -----
  print                         : 0.053 s
  write to file (+fsync)        : 0.108 s
  print with stdout = /dev/null : 0.045 s
  </code></pre>
  
  <p>So it isn't a python trick, it is just the terminal.  I always knew dumping output to /dev/null sped things up, but never figured it was that significant!</p>
  
  <p>It amazes me how slow the tty is.  How can it be that writing to physical disk is WAY faster than writing to the "screen" (presumably an all-RAM op), and is effectively as fast as simply dumping to the garbage with /dev/null?</p>
  
  <p><a href="http://www.linusakesson.net/programming/tty/index.php" rel="noreferrer">This link</a> talks about how the terminal will block I/O so it can <em>"parse [the input], update its frame buffer, communicate with the X server in order to scroll the window and so on"</em>... but I don't fully get it.  What can be taking so long?</p>
  
  <p>I expect there is no way out (short of a faster tty implementation?) but figure I'd ask anyway.</p>
  
  <hr>
  
  <p>UPDATE: after reading some comments I wondered how much impact my screen size actually has on the print time, and it does have some significance.  The really slow numbers above are with my Gnome terminal blown up to 1920x1200.  If I reduce it very small I get...</p>
  
  <pre><code>-----
  timing summary (100k lines each)
  -----
  print                         : 2.920 s
  write to file (+fsync)        : 0.121 s
  print with stdout = /dev/null : 0.048 s
  </code></pre>
  
  <p>That is certainly better (~4x), but doesn't change my question.  It only <em>adds</em> to my question as I don't understand why the terminal screen rendering should slow down an application writing to stdout.  Why does my program need to wait for screen rendering to continue?</p>
  
  <p>Are all terminal/tty apps not created equal?  I have yet to experiment.  It really seems to me like a terminal should be able to buffer all incoming data, parse/render it invisibly, and only render the most recent chunk that is visible in the current screen configuration at a sensible frame rate.  So if I can write+fsync to disk in ~0.1 seconds, a terminal should be able to complete the same operation in something of that order (with maybe a few screen updates while it did it).</p>
  
  <p>I'm still kind of hoping there is a tty setting that can be changed from the application side to make this behaviour better for programmer.  If this is strictly a terminal application issue, then this maybe doesn't even belong on StackOverflow?</p>
  
  <p>What am I missing?</p>
  
  <hr>
  
  <p>Here is the python program used to generate the timing:</p>
  
  <pre><code>import time, sys, tty
  import os
  
  lineCount = 100000
  line = "this is a test"
  summary = ""
  
  cmd = "print"
  startTime_s = time.time()
  for x in range(lineCount):
      print line
  t = time.time() - startTime_s
  summary += "%-30s:%6.3f s\n" % (cmd, t)
  
  #Add a newline to match line outputs above...
  line += "\n"
  
  cmd = "write to file (+fsync)"
  fp = file("out.txt", "w")
  startTime_s = time.time()
  for x in range(lineCount):
      fp.write(line)
  os.fsync(fp.fileno())
  t = time.time() - startTime_s
  summary += "%-30s:%6.3f s\n" % (cmd, t)
  
  cmd = "print with stdout = /dev/null"
  sys.stdout = file(os.devnull, "w")
  startTime_s = time.time()
  for x in range(lineCount):
      fp.write(line)
  t = time.time() - startTime_s
  summary += "%-30s:%6.3f s\n" % (cmd, t)
  
  print &gt;&gt; sys.stderr, "-----"
  print &gt;&gt; sys.stderr, "timing summary (100k lines each)"
  print &gt;&gt; sys.stderr, "-----"
  print &gt;&gt; sys.stderr, summary
  </code></pre>