我在x86_64 Linux上使用GCC 4.8和glibc 2.19。
在使用a different question的不同输入法时,我比较了fscanf
和sscanf
。具体来说,我会直接在标准输入上使用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 perf工具来衡量这一点。例如,对于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
具有更好的记忆位置(反复使用相同的缓冲区),但这不会产生如此大的差异。
有没有人对这种奇怪的差异有任何见解?
答案 0 :(得分:18)
sscanf()将您传入的字符串转换为_IO_FILE*
,使字符串看起来像“文件”。这是相同的内部_IO_vfscanf()可以用于字符串和文件*。
但是,作为转换的一部分,在_IO_str_init_static_internal()函数中完成,它在输入字符串上调用__rawmemchr (ptr, '\0');
本质上是一个strlen()调用。这个转换是在每次调用sscanf()时完成的,因为你的输入缓冲区相当大,所以它会花费相当多的时间来计算输入字符串的长度。
使用fmemopen()从输入字符串创建FILE *并使用fscanf()可能是另一种选择。
答案 1 :(得分:7)
在执行任何其他操作之前,看起来glibc的sscanf()
会扫描源字符串的长度。
sscanf()
(在stdio-common/sscanf.c
中)本质上是对_IO_vsscanf()
(libio/iovsscanf.c
)调用的包装器。 _IO_vsscanf()
所做的第一件事就是通过调用_IO_strfile
(在_IO_str_init_static_internal()
中)来初始化自己的libio/strops.c
结构,该{{1}}计算字符串的长度,如果它是&#39}。未提供。