14

I have a code where I currently print a lot of diagnostic messages to terminal. Does anybody have any idea how much this slows down my code? Would I get a big speed increase by piping the output to file, e.g. instead of running:

./my_program

i run

./my_program > output.log

Also, would I get a further speed increase by replacing cout with ofstream and writing to file directly?

EDIT: Let's assume I am writing to /dev/shm, disk access speed not really an issue.

user788171
  • 16,753
  • 40
  • 98
  • 125
  • Interesting question +1 from me. time it, `time ./my_program > output.log 2>&1` and see what results you get, then compare it with `time ./my_program > output.log 2>&1 &` (fork in background), and use `multitail output.log` to see the output from another terminal. It depends on the cpu load, services, processes, RAM... – t0mm13b Jul 14 '12 at 21:46
  • 11
    You could just profile it and find out... (!) – Oliver Charlesworth Jul 14 '12 at 21:52
  • I know that back in the old days of Windows 95, you could press Alt+Enter to switch the DOS box to text mode, and then programs ran _much_ faster. – Mr Lister Jul 17 '12 at 18:22
  • Try printing on a different thread and free your main program thread. That should significantly speed up your performance. Check this: https://superuser.com/a/312955 – Nilav Baran Ghosh Feb 01 '18 at 16:59
  • You might be able to get faster console output by buffering and/or by changing the buffer size. See https://en.cppreference.com/w/cpp/io/c/setvbuf – L. Scott Johnson Jun 18 '19 at 17:36

6 Answers6

10

Yes, rendering to screen takes longer than writing to file.
In windows its even slower as the program rendering is not the program that is running, so there are constantly messages sent between processes to get it drawn.
I guess its same in linux since virtual terminal is on a different process than the one that is running.

Daniel
  • 30,896
  • 18
  • 85
  • 139
2

It certainly can be. Printing to a terminal involves rendering and other things (non-trivial) and is typically buffered a lot less. The OS and stream implementation can do a lot more buffering and caching with file I/O.

Puppy
  • 144,682
  • 38
  • 256
  • 465
2

How much of a speed hit you get will depend on several factors. The Windows console, for instance, is notorously slow. ofstream might be slighlty faster than cout, depending on yor libc++ implementation (different buffer sizes, thread synchronization, ...)

snemarch
  • 4,958
  • 26
  • 38
1

It really depends on how much you are printing.

If your program is printing 50 or more lines per second then I bet output starts to become significant.

Output to a file is definitely much faster than printing to a terminal, though different terminal programs will vary significantly in their speed, depending on how much rendering they are doing and what they use for the rendering api.

I very much doubt there is any significant performance difference for cout vs. ofstream for performance of terminal printing or even output to a file. There might be a very small performance gain if you wrote log lines using fwrite. Ultimately things like cout will call fwrite, so you can get a small improvement by just calling down to that lowest level yourself.

Finally - output streams like cout are faster than error streams like cerr. Cout will do more buffering than cerr, performance can be significantly faster. But it looks like you are using cout already.

Rafael Baptista
  • 11,181
  • 5
  • 39
  • 59
0

This is hard to say without measuring a particular system, but I suspect that writing to a file would in fact be faster than writing to the display (files don't have to scroll etc).

Depending on your OS, system, and libraries, writing to an ofstream directly could increase performance further if it uses a different buffering scheme compared to cout but it might have no effect whatsoever. The only way to know for sure is to profile your code (as already suggested in a comment).

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

Generally, yes. If you don't have to write to the terminal, you can use a file. You can also use /dev/null if there is no need to see the output.(For example, to measure real speed ...)