ios::sync_with_stdio(false)提高C++读写速度
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 type ios_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 type ios_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 (see ios::tie), which indicates that cout‘s buffer is flushed (see ostream::flush) before each i/o operation performed on cin.
(来源:http://www.cplusplus.com/reference/iostream/cin/ 网址)
当传入参数为0时,实现cin与cout独立运行,不会等待,提高cin速度
全局域的通过静态常量初始化一个lambda的方式实现对此代码的优先调用
static const int _ = []() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); return 0; }();
Techie亮博客,转载请注明:Coologic » ios::sync_with_stdio(false)提高C++读写速度
- 点赞
- 收藏
- 分享
- 文章举报
- 探寻C++最快的读取文件的方案std::ios::sync_with_stdio(false) (转)
- C,C++,C++中添加ios::sync_with_stdio(false);代码大小和耗时测评
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- C++ 里利用 std::ios::sync_with_stdio(false) 解决TLE问题
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- std::ios::sync_with_stdio(false);--------探寻C++最快的读取文件的方案
- 关于C++中ios::sync_with_stdio(false);
- 转载——探寻C++最快的读取文件的方案std::ios::sync_with_stdio(false)——转载
- PAT乙级真题 1028 人口普查 C++实现(使用std::ios::sync_with_stdio(false))
- 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
- 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
- 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- C++输入输出流加速器,关闭同步流,ios::sync_with_stdio(false)和 cin.tie(0)
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- C++ 里利用 std::ios::sync_with_stdio(false) 解决TLE问题
- ios::sync_with_stdio(false) 改变C++流输入输出的速度
- ios::sync_with_stdio(false);