博客地址已更改,文章数量较多不便批量修改,若想访问源文请到 coologic博客 查阅,网址:www.coologic.cn
如本文记录地址为 techieliang.com/A/B/C/ 请改为 www.coologic.cn/A/B/C/ 即可查阅
版权声明:若无来源注明,Techie亮博客文章均为原创。 转载请以链接形式标明本文标题和地址:本文标题:ios::sync_with_stdio(false)提高C++读写速度 本文地址:http://techieliang.com/2017/11/275/
C++为了兼容C,默认使iostream与stdio关联,使cin与scanf、cout和printf保持同步,保证混用过程中文件指针不混乱。
此方式会造成性能损失,导致使用cin/cout效率远远低于使用scanf/printf。若保证程序只是用一套指令,可取消同步:
- std::ios::sync_with_stdio(false);
取消后使用cin/cout速度有明显提升,但注意不要混用iostream与stdio
- std::cin.tie(0);
C++98:In terms of static initialization order,cin is guaranteed to be properly constructed and initialized no later than the first time an object of typeios_base::Init is constructed.
C++11:In terms of static initialization order,cin is guaranteed to be properly constructed and initialized no later than the first time an object of typeios_base::Init is constructed, with the inclusion of<iostream> counting as at least one initialization of such objects with static duration.
cin is tied to the standard output stream cout (seeios::tie), which indicates thatcout‘s buffer is flushed (seeostream::flush) before each i/o operation performed oncin.
(来源:http://www.cplusplus.com/reference/iostream/cin/ 网址)
当传入参数为0时,实现cin与cout独立运行,不会等待,提高cin速度