printf 减慢了我的程序 [英] printf slows down my program

查看:22
本文介绍了printf 减慢了我的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的 C 程序来计算哈希(用于哈希表).我希望代码看起来很干净,但有一些与它无关的东西困扰着我.

I have a small C program to calculate hashes (for hash tables). The code looks quite clean I hope, but there's something unrelated to it that's bugging me.

我可以在大约 0.2-0.3 秒内轻松生成大约一百万个哈希值(以/usr/bin/time 为基准).但是,当我在 for 循环中 printf() 调用它们时,程序会减慢到大约 5 秒.

I can easily generate about one million hashes in about 0.2-0.3 seconds (benchmarked with /usr/bin/time). However, when I'm printf()inging them in the for loop, the program slows down to about 5 seconds.

  1. 这是为什么?
  2. 如何让它更快?mmapp() 可能是标准输出?
  3. stdlibc 在这方面是如何设计的,如何改进?
  4. 内核如何更好地支持它?需要如何修改才能真正提高本地文件"(套接字、管道等)的吞吐量?

我期待着有趣而详细的回复.谢谢.

I'm looking forward for interesting and detailed replies. Thanks.

PS:这是一个编译器构建工具集,所以不要害羞进入细节.虽然这与问题本身无关,但我只是想指出我感兴趣的细节.

PS: this is for a compiler construction toolset, so don't by shy to get into details. While that has nothing to do with the problem itself, I just wanted to point out that details interest me.

附录

我正在寻找解决方案和解释的更多程序化方法.确实,管道可以完成这项工作,但我无法控制用户"做什么.

I'm looking for more programatic approaches for solutions and explanations. Indeed, piping does the job, but I don't have control over what the "user" does.

当然,我现在正在进行测试,普通用户"不会这样做.但这并没有改变一个事实,即简单的 printf() 会减慢进程的速度,这是我试图为其寻找最佳编程解决方案的问题.

Of course, I'm doing a testing right now, which wouldn't be done by "normal users". BUT that doesn't change the fact that a simple printf() slows down a process, which is the problem I'm trying to find an optimal programmatic solution for.

附录 - 惊人的结果

参考时间用于 TTY 内的普通 printf() 调用,大约需要 4 分 20 秒.

The reference time is for plain printf() calls inside a TTY and takes about 4 mins 20 secs.

在/dev/pts(例如 Konsole)下进行测试将输出加快到大约 5 秒.

Testing under a /dev/pts (e.g. Konsole) speeds up the output to about 5 seconds.

在我的测试代码中使用 setbuffer() 大小为 16384 所需的时间大致相同,8192 几乎相同:大约 6 秒.

It takes about the same amount of time when using setbuffer() in my testing code to a size of 16384, almost the same for 8192: about 6 seconds.

setbuffer() 在使用时显然没有效果:它需要相同的时间(在 TTY 上大约 4 分钟,在 PTS 上大约 5 秒).

setbuffer() has apparently no effect when using it: it takes the same amount of time (on a TTY about 4 mins, on a PTS about 5 seconds).

令人惊讶的是,如果我在 TTY1 上开始测试,然后切换到另一个 TTY,它确实需要与在 PTS 上相同的时间:大约5 秒.

The astonishing thing is, if I'm starting the test on TTY1 and then switch to another TTY, it does take just the same as on a PTS: about 5 seconds.

结论:内核做了一些与可访问性和用户友好性有关的事情.哈!

Conclusion: the kernel does something which has to do with accessibility and user friendliness. HUH!

通常,无论您在 TTY 处于活动状态时盯着它看,还是切换到另一个 TTY,它都应该同样慢.

Normally, it should be equally slow no matter if you stare at the TTY while its active, or you switch over to another TTY.

教训:运行输出密集型程序时,切换到另一个 TTY!

Lesson: when running output-intensive programs, switch to another TTY!

推荐答案

无缓冲输出很慢.

默认情况下,stdout 是全缓冲的,但是当连接到终端时,stdout 要么是无缓冲的,要么是行缓冲的.

By default stdout is fully-buffered, however when attached to terminal, stdout is either unbuffered or line-buffered.

尝试使用 setvbuf()stdout 开启缓冲,如下所示:

Try to switch on buffering for stdout using setvbuf(), like this:

char buffer[8192];

setvbuf(stdout, buffer, _IOFBF, sizeof(buffer));

这篇关于printf 减慢了我的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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