Why is it slower to print directly to console/terminal than redirecting?

69 views Asked by At

Why does it take significantly more time to print multiple lines to the terminal rather than redirecting it to a file which seems to be almost instant ?

1

There are 1 answers

0
that other guy On BEST ANSWER

Primarily, terminals are just plain slow. For every update, they have to:

  1. Parse and interpret any control codes.
  2. Parse, interpret and render any escape codes.
  3. Interpret and account for any multibyte and composed characters.
  4. Update appropriate screen and scroll buffers.
  5. Render this with appropriate fonts.

and possibly do all of the above over again if you use screen or tmux.

Meanwhile, for redirecting to a file, you just have to:

  1. Dump data into RAM (for later writeback to storage).

    This step is so minor that it doesn't even register on the terminal's checklist.

This is not something people optimize for, because the speed of your terminal is rarely an issue. The difference between terminals can be 50x (VGA vs fbcon back in the day), and you can time it with a simple time cat somebigfile.txt.