我写了一些最小的代码来测试设置std::ios_base::sync_with_stdio(false)
:
#include <iostream>
#include <string>
int main() {
std::string line;
#ifdef NO_SYNC_STDIO
std::ios_base::sync_with_stdio(false);
#endif
#ifdef READ
while (std::getline(std::cin, line))
#else
line = "123456";
for (int i=0; i<10000000; i++)
#endif
#ifdef WRITE
std::cout << line << "\n";
#else
1;
#endif
return 1;
}
这些是time [yes 123456 | sed 10000000q | ] ./stdstream-dummy > /dev/null
的结果:
-DREAD -DWRITE -DREAD -DWRITE
real 24.963s 4.622s 0.950s
user 22.514s 6.732s 0.942s
sys 4.825s 0.313s 0.008s
使用-DNO_SYNC_STDIO
:
-DREAD -DWRITE -DREAD -DWRITE
real 38.327s 1.865s 1.223s
user 29.835s 3.990s 1.180s
sys 10.875s 0.351s 0.043s
可以看出,如果我必须写,时间就会降低一点。任何见解?
一些额外信息:
$ g++ --version
g++ (GCC) 3.4.3 (csl-sol210-3_4-branch+sol_rpath)
Copyright (C) 2004 Free Software Foundation, Inc.
$ uname -a
SunOS cfprepagop01 5.10 Generic_148888-05 sun4v sparc sun4v
$ ldd stdstream-dummy
libstdc++.so.6 => /usr/sfw/lib/libstdc++.so.6
libm.so.2 => /lib/libm.so.2
libgcc_s.so.1 => /usr/sfw/lib/libgcc_s.so.1
libc.so.1 => /lib/libc.so.1
/lib/libm/libm_hwcap1.so.2
/platform/sun4v/lib/libc_psr.so.1