无聊的输入输出测试
无聊的输入输出测试
机器
Ubuntu 18.04 LTS amd64 和 C++14 以及均无 O2。
测试
cin 输入 5e5 个六位数。
- 无特殊 147ms
cin.tie(0)131msios_base::sync_with_stdio(0)46msios::sync_with_stdio(0)46ms- 2+3 43ms
- 2+4 43ms
cin.tie(0)->sync_with_stdio(0)42ms
cout 输出 5e6 个六位数。
- 有
endl都 >10s - 只有
'\n'46ms cout.tie(0)48msios_base::sync_with_stdio38msios::sync_with_stdio39ms- 3+4 39ms
- 3+5 37ms
cout.tie(0)->sync_with_stdio(0)49ms
scanf 输入 5e5 个六位数。
- 无特殊 12ms
ios_base::sync_with_stdio12msios::sync_with_stdio12mscin.tie(0)->sync_with_stdio(0)15ms
printf 输出 5e6 个六位数。
- 无特殊 48ms
ios_base::sync_with_stdio49msios::sync_with_stdio49mscout.tie(0)->sync_with_stdio(0)54ms
快读输入 5e5 个六位数(cin/cout 均开满)
inline ll read()
{
static char ch, last;
static ll res;
ch = last = res = 0;
while (!isdigit(ch))
{
last = ch, ch = getchar();
}
while (isdigit(ch))
{
res = res * 10 + ch - '0', ch = getchar();
}
return last == '-' ? -res : res;
}
- 如上 6ms
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)5mscin.read(buf, 1 << 20)6ms
快输输出 5e6 个六位数
char _stk[20];
int _tp = 0;
inline void write(long long x)
{
do
{
_stk[++_tp] = x % 10, x /= 10;
} while (x);
while (_tp)
{
putchar(_stk[_tp--] | 48);
}
}
- 如上 65ms
fwrite(obuf, O - obuf, 1, stdout)28mscout.write(buf, pout - buf)30ms
总结
输入:
fread() < cin.read() < read() < scanf() < cin(no tie no sync) < cin(no sync) < cin(no tie) < cin
输出:
fwrite() < cout.write() < cout(no tie no sync no endl) < cout(no sync no endl) < printf() < cout(no tie no endl) < cout(no endl) < write < cout
组合:fread+fwrite < cin.read()+cout.write() < read()+printf() < no sync no tie no endl cin+cout