Osx/Linux, Slow Down the Output from Terminal

Terminal output is slower than file write?

This is Linux that is causing the slowdown. The same would happen if you printed the output in a windows console. Each flush of the output is obliging the program to halt, the operating system to update its window, and the program to be given control to continue again. If these flushes are numerous and often, it may indeed slow down things quite a bit.

Writing to a file still requires the operating system to step in, but there is no overhead beyond what you'd normally see with a program that reads and writes files. That said, writing to the console is a handy tool for debugging a program and you should use it. Just be sure that when it runs on the production machine, it writes to a file log instead.

Altering scroll speed in Linux in MacOSX

control-A = beginning of line

control-E = end of line

option-arrows = forward or backwards one word at a time. This is the one that really makes the difference.

Slowing C++ output on terminal

You can use standard C++ (C++11):

#include <thread>
#include <chrono>
#include <iostream>

int main() {
while (true) {
// draw loop
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}

Alternatively, you could use a library that lets you specify an interval at which to call your draw function. OS X has Grand Central Dispatch (a.k.a. libdispatch). Using GCD you could create a dispatch timer source that calls your draw function with a specified frequency.

dispatch_source_t timer = dispatch_source_create(
DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());

dispatch_source_set_timer(timer, DISPATCH_TIME_NOW,
duration_cast<nanoseconds>(milliseconds(20)).count(),
duration_cast<nanoseconds>(milliseconds( 5)).count());
// the API is defined to use nanoseconds, but I'd rather work in milliseconds
// so I use std::chrono to do the conversion above

dispatch_source_set_event_handler(timer,
[]{ your_draw_function(); });
// I'm not sure if GCC 4.7 actually supports converting C++11 lambdas to
// Apple's C blocks, or if it even supports blocks. Clang supports this.

dispatch_resume(timer);

dispatch_main();

libdispatch reference

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

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.

Performance difference of iostream console output between Windows and OSX?

std::endl flush the line. It is quite expensive to do this.

Try to do :

std::cout << i << '\n';

In most other usual interactive I/O scenarios, std::endl is redundant when used with std::cout because any input from std::cin, output to std::cerr, or program termination forces a call to std::cout.flush().

Use of std::endl in place of '\n', encouraged by some sources, may significantly degrade output performance.

Source


EDIT :
Output operation are costly and depend on external factors. This is why it is slow here. For example, the terminal application being used can be factor of some performance issues.

You can avoid that by redirecting the output to /dev/null/ :

./a.out > /dev/null

On output performance, you can read this : http://codeforces.com/blog/entry/5217



Related Topics



Leave a reply



Submit