title: C: Why is a fprintf(stdout,....) so slow?
body: <p>I still often use console output to get ideas what's going on in my code.
  I know this may be a bit old fashion, but I also use this to "pipe" stdout
  into log files etc.</p>
  
  <p>However, it turns out that the output to the console is slowed down for some 
  reason. I was wondering if someone can explain why an fprintf() to a console
  window appears to be sort of blocking.</p>
  
  <p>What I've done/diagnosed so far:</p>
  
  <ol>
  <li><p>I measured the time a simple 
  <code>fprintf(stdout,"quick fprintf\n");</code>
  It needs: 0.82ms (in average). This is considered by far too long since a <code>vsprintf_s(...)</code> writes the same output into a string in just a few microseconds. Therefore there must be some blocking specifically to the console.</p></li>
  <li><p>In oder to escape from the blocking I have used <code>vsprintf_s(...)</code> to copy my output into a fifo alike data structure. The data structure is protected by a critical section object. A separate thread is then unqueing the data structure by putting the queued output to the console.</p></li>
  <li><p>One further improvement I could obtain by the introduction of pipe services.
  The output of my program (supposed to end up in a console window) goes the following way:</p>
  
  <ul>
  <li>A <code>vsprintf_s(...)</code> formats the output to simple strings.</li>
  <li>The strings are queued into a fifo alike data structure, a linked list sructure for example. This data structure is protected by a critical section object.</li>
  <li>A second thread dequeues the data structure by sending the output strings to a named pipe.</li>
  <li>A second process reads the named pipe and puts the strings again into a fifo alike data
  structure. This is needed to keep the reading away from the blocking output to the console.
  The reading process is fast at reading the named pipe and monitors the fill level of the pipes buffer continuously. </li>
  <li>A second thread in that second process finally dequeues the data structure by <code>fprintf(stdout,...)</code> to the console.</li>
  </ul></li>
  </ol>
  
  <p>So I have two processes with at least two threads each, a named pipe between them, and fifo alike data structures on both sides of the pipe to avoid blocking in the event of pipe buffer full.</p>
  
  <p>That is a lot of stuff to just make sure that console output is "non-blocking". But the result is
  not too bad. My main program can write complex fprintf(stdout,...) within just a few microseconds.</p>
  
  <p>Maybe I should have asked earlier: Is there some other (easier!) way to have nonblocking console output?</p>