gpt4 book ai didi

c - 为什么 glibc 的 sscanf 在 Linux 上比 fscanf 慢很多?

转载 作者:太空狗 更新时间:2023-10-29 16:29:38 25 4
gpt4 key购买 nike

我在 x86_64 Linux 上使用 GCC 4.8 和 glibc 2.19。

在玩a different question的不同输入法时,我比较了 fscanfsscanf。具体来说,我会直接在标准输入上使用 fscanf:

char s[128]; int n;

while (fscanf(stdin, "%127s %d", s, &n) == 2) { }

或者我会先将整个输入读入缓冲区,然后使用 sscanf 遍历缓冲区。 (将所有内容读入缓冲区需要很短的时间。)

char s[128]; int n;
char const * p = my_data;

for (int b; sscanf(p, "%127s %d%n", s, &n, &b) == 2; p += b) { }

令我惊讶的是,fscanf 版本的速度大大 更快。例如,使用 fscanf 处理几万行需要这么长时间:

10000       0.003927487 seconds time elapsed
20000 0.006860206 seconds time elapsed
30000 0.007933329 seconds time elapsed
40000 0.012881912 seconds time elapsed
50000 0.013516816 seconds time elapsed
60000 0.015670432 seconds time elapsed
70000 0.017393129 seconds time elapsed
80000 0.019837480 seconds time elapsed
90000 0.023925753 seconds time elapsed

现在与 sscanf 相同:

10000       0.035864643 seconds time elapsed
20000 0.127150772 seconds time elapsed
30000 0.319828373 seconds time elapsed
40000 0.611551668 seconds time elapsed
50000 0.919187459 seconds time elapsed
60000 1.327831544 seconds time elapsed
70000 1.809843039 seconds time elapsed
80000 2.354809588 seconds time elapsed
90000 2.970678416 seconds time elapsed

我使用 Google 性能工具来衡量这一点。例如,对于 50000 行,fscanf 代码需要大约 50M 周期,sscanf 代码大约需要 3300M 周期。因此,我使用 perf record/perf report 分解了排名靠前的调用站点。使用 fscanf:

 35.26%  xf  libc-2.19.so         [.] _IO_vfscanf
23.91% xf [kernel.kallsyms] [k] 0xffffffff8104f45a
8.93% xf libc-2.19.so [.] _int_malloc

sscanf:

 98.22%  xs  libc-2.19.so         [.] rawmemchr
0.68% xs libc-2.19.so [.] _IO_vfscanf
0.38% xs [kernel.kallsyms] [k] 0xffffffff8104f45a

所以几乎所有使用sscanf的时间都花在了rawmemchr上!为什么是这样? fscanf 代码如何避免这种开销?

我试着搜索这个,但我能想到的最好的是 this discussion锁定的 realloc 调用,我认为这里不适用。我还认为 fscanf 具有更好的内存局部性(反复使用相同的缓冲区),但这不会产生如此大的差异。

有人对这种奇怪的差异有任何见解吗?

最佳答案

sscanf() 将您传入的字符串转换为 _IO_FILE*,使字符串看起来像一个"file"。这是因为相同的内部 _IO_vfscanf() 可用于字符串和 FILE*。

但是,作为该转换的一部分,它在 _IO_str_init_static_internal() 函数中完成,它调用 __rawmemchr (ptr, '\0'); 本质上是对您的输入字符串的 strlen() 调用。每次调用 sscanf() 时都会进行此转换,并且由于您的输入缓冲区相当大,因此它将花费大量时间来计算输入字符串的长度。

使用 fmemopen() 从输入字符串创建一个 FILE* 并使用 fscanf() 可能是另一种选择。

关于c - 为什么 glibc 的 sscanf 在 Linux 上比 fscanf 慢很多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23923924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com