C++ does printing to terminal significantly slow down code?(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++ 打印到终端会显着减慢代码速度吗?


- STL 中有 dereference_iterator 吗? 2022-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 从python回调到c++的选项 2022-11-16