C ++打印到终端显着减慢代码? [英] C++ does printing to terminal significantly slow down code?

查看:192
本文介绍了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替换为stream并写入文件,我可以进一步提高速度直接?

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.

推荐答案

是的,渲染到屏幕比写入文件需要更长时间。

在窗口中,它的速度更慢,因为程序渲染不是正在运行的程序,

我猜它在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 ++打印到终端显着减慢代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆