title: printf slows down my program
body: <p>I have a small C program to calculate hashes (for hash tables). The code looks quite clean I hope, but there's something unrelated to it that's bugging me.</p>
  
  <p>I can easily generate about one million hashes in about 0.2-0.3 seconds (benchmarked with /usr/bin/time). However, when I'm printf()inging them in the for loop, the program slows down to about 5 seconds.</p>
  
  <ol>
  <li>Why is this?</li>
  <li>How to make it faster? mmapp()ing stdout maybe?</li>
  <li>How is stdlibc designed in regards to this, and how may it be improved?</li>
  <li>How could the kernel support it better? How would it need to be modified to make the throughput on local "files" (sockets,pipes,etc) REALLY fast?</li>
  </ol>
  
  <p>I'm looking forward for interesting and detailed replies. Thanks.</p>
  
  <p>PS: this is for a compiler construction toolset, so don't by shy to get into details. While that has nothing to do with the problem itself, I just wanted to point out that details interest me.</p>
  
  <p><strong>Addendum</strong></p>
  
  <p>I'm looking for more programatic approaches for solutions and explanations. Indeed, piping does the job, but I don't have control over what the "user" does.</p>
  
  <p>Of course, I'm doing a testing right now, which wouldn't be done by "normal users". BUT that doesn't change the fact that a simple printf() slows down a process, which is the problem I'm trying to find an optimal programmatic solution for.</p>
  
  <hr>
  
  <p><strong>Addendum - Astonishing results</strong></p>
  
  <p>The reference time is for plain printf() calls inside a TTY and takes about 4 mins 20 secs.</p>
  
  <p>Testing under a /dev/pts (e.g. Konsole) speeds up the output to about 5 seconds.</p>
  
  <p>It takes about the same amount of time when using setbuffer() in my testing code to a size of 16384, almost the same for 8192: about 6 seconds.</p>
  
  <p>setbuffer() has <strong>apparently</strong> no effect when using it: it takes the same amount of time (on a TTY about 4 mins, on a PTS about 5 seconds).</p>
  
  <p><strong>The astonishing thing is</strong>, if I'm starting the test on TTY1 and then <em>switch to another TTY</em>, it does take just the same as on a PTS: about 5 seconds.</p>
  
  <p><strong>Conclusion</strong>: the kernel does something which has to do with accessibility and user friendliness. HUH!</p>
  
  <p>Normally, it should be equally slow no matter if you stare at the TTY while its active, or you switch over to another TTY.</p>
  
  <hr>
  
  <p><strong>Lesson</strong>: when running output-intensive programs, switch to another TTY!</p>