【发布时间】:2012-08-07 19:01:02
【问题描述】:
这个程序每行打印 65k 字节。
我用./a.out | pv >/dev/null 测量吞吐量,得到大约 3 GB/s。
只要我将线路长度更改为 70k,吞吐量就会下降到 ~ 1 GB/s。
哪个瓶颈(CPU 缓存、libc 特质等)我遇到了什么问题?
#include <stdio.h>
#include <string.h>
#define LEN 65000 // high throughput
// #define LEN 70000 // low throughput
int main ()
{
char s[LEN]; memset(s, 'a', LEN-1); s[LEN-1] = '\0';
while (1)
printf ("%s\n", s);
}
更新:我在 Ubuntu 12.04 64 位上运行它,它有 EGLIBC 2.15,在 Core i5-2520M 上。
更新:puts (s) 也有同样的问题。
【问题讨论】:
-
同样重要——你使用的是什么 libc,什么版本?
-
可能是 libc 的内部缓冲区,尤其是在
printf内部。puts怎么样? -
我不确定这是否是有用的信息,但
65000适合两个字节,而70000需要三个。对于比我知识渊博的人来说,这可能是一个起点? -
你需要在profiler下运行你的程序来回答这个问题。
-
瓶颈在你的a.out,还是在pv?
标签: c performance caching printf benchmarking