C++ 打印到终端会显着减慢代码速度吗?

C++ does printing to terminal significantly slow down code?(C++ 打印到终端会显着减慢代码速度吗?)

本文介绍了C++ 打印到终端会显着减慢代码速度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,我目前在其中向终端打印了很多诊断消息.有人知道这会减慢我的代码速度吗?通过将输出管道传输到文件,我是否会大大提高速度,例如而不是运行:

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

我跑

./my_program > output.log

另外,通过将 cout 替换为 ofstream 并直接写入文件,我会进一步提高速度吗?

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

假设我正在写入/dev/shm,磁盘访问速度不是真正的问题.

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

推荐答案

是的,渲染到屏幕比写入文件花费的时间更长.
在 Windows 中它甚至更慢,因为程序渲染不是正在运行的程序,因此进程之间不断发送消息以获取它.
我猜它在 linux 中是一样的,因为虚拟终端与正在运行的进程位于不同的进程上.

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.

这篇关于C++ 打印到终端会显着减慢代码速度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++ 打印到终端会显着减慢代码速度吗?

基础教程推荐