一、sync_with_stdio()

这个函数是一个“是否兼容stdio”的开关,C++为了兼容C,保证程序在使用了std::printf和std::cout的时候不发生混乱,将输出流绑在了一起。

在IO之前将stdio接触绑定,可以大大提高IO效率。在操作大数据时,cin,cout的速率也能很快了。

现在,我们通过比较解除绑定前后cin,printf的速率来实际体验下sync_with_stdio()的作用。

首先,我们先产生1000万个随机数作为测试数据。然后,分别用cin,scanf来读取数据,比较其效率

  • data.cpp,产生1000万个随机数存贮在data.txt中,大概55M左右
 /*
本程序实现的功能:
生成1000万个可能重复的随机数,用作测试数据
并计算生成这些数据所用的时间
*/
#include <iostream>
#include <ctime>
using namespace std;
#define SELF_RAND_MAX 0x7FFFFFFF
int main()
{ clock_t start_time = clock(); srand(unsigned(time())); const int MAX = ;
const int MIN = ; freopen("data.txt","w",stdout); for(int i = ; i < ; ++i){
unsigned long data = rand() % (MAX - MIN + ) + MIN;
cout << data << ' ';
}
fclose(stdout);
freopen("time.txt","w",stdout);
cout << endl
<< "---the end---"
<< endl;
//CLOCKS_PER_SEC控制精度,在windows环境下是1000,linux下是多少?
//简单地说windows下是毫秒级,而linux下好像是纳秒级
cout << "elapsed time:" << double(clock() - start_time) / CLOCKS_PER_SEC
<< 's' << endl;
fclose(stdout);
}
  • test.cpp, 测试程序,读取data.txt中的数据,比较cin, printf的效率
 #include <iostream>
#include <ctime>
using namespace std; void scanf_read();
void cin_read(); const int MAXIN = ;
int numbers[MAXIN]; int main () {
iostream::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
clock_t start_time = clock();
//do something
scanf_read();
//cin_read();
cout << "read time:"
<< double(clock() - start_time) / CLOCKS_PER_SEC
<< endl;
} void scanf_read() {
freopen("data.txt","r",stdin);
for (int i = ; i < MAXIN; ++i) {
scanf("%d", &numbers[i]);
}
} void cin_read() {
freopen("data.txt", "r", stdin);
for (int i = ; i < MAXIN; ++i) {
cin >> numbers[i];
}
}

运行结果(win7 32 i3处理器,mingw 4.9.2):

  • 解除绑定前

通过scanf来读取1000万个随机数需要的时间:16.323s

通过cin来读取1000万个随机数需要的时间:24.361s

我们可以看到,cin的读取时间整整比scanf满了8秒。

  • 解除绑定后

通过scanf读取1000万个数据需要的时间:16.29s。

通过cin来读取1000万个随机数需要的时间:4.946s,我们看到cin的读取时间整整缩短了20秒,比scanf的读取时间还快乐10秒。

  • 解除绑定,并cin.tie(nullptr), cout.tie(nullptr)

通过cin来读取1000万个随机数需要的时间:4.861s,数值上来看,比只解除绑定好像快了一丢丢。但效果不明显。这里提到了tie()函数,下面就来看看tie()函数。

二、tie()

tie()用来绑定stream,空参数则返回当前的输出流指针。

直接上程序,看看其是如何表现的。

  • tie.cpp, 测试tie的实现效果。
 #include <iostream>
#include <fstream>
using namespace std; int main () {
ostream *prevstr;
ofstream ofs; ofs.open("test.txt");
cout << "Output to the screen" << endl; *cin.tie() << "Output to the screem, too" << endl; //null parameters, return the default output stream, i.e. cout prevstr = cin.tie(&ofs); // cin bind to ofs, and return the pre output stream, i.e. cout
*cin.tie() << "Output to the file, test.txt" << endl; *cin.tie(prevstr) << "Output to the file, too";
*cin.tie() << "Output to the screen, again"; ofs.close(); return ; }

运行结果(环境同一):

控制台输出:

Output to the screen
Output to the screen, too
Output to the screen, again

文件输出:

对照程序结果,我们可以很清楚的明白tie()的作用。就不多讲了。

PS:Open live writer怎么添加“代码插入”插件啊!!!看见有人用SyntaxHighlighter,自己折腾半天也没把SyntaxHighlighter和Open live writer整合在一起,傻傻的在网页上编辑。

1, sync_with_stdio(), tie()的应用的更多相关文章

  1. cin.tie与sync_with_stdio加速输入输出

    在LeetCode上练习习题的时候每次AC之后都会去看别人的代码,几乎每次都能遇到cin.tie与sync_with_stdio.类似这样: static auto x = [](){ std::io ...

  2. 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流

    原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.ht ...

  3. std::ios::sync_with_stdio和tie()——给cin加速

    平时在Leetcode上刷题的时候,总能看到有一些题中最快的代码都有这样一段 static const auto init = []() { std::ios::sync_with_stdio(fal ...

  4. C++输入输出流加速器,关闭同步流,ios::sync_with_stdio(false)和 cin.tie(0)

    leetcode练习时,总会发现运行时间短的代码都会有类似: static int x=[](){ std::ios::sync_with_stdio(false); cin.tie(NULL); ; ...

  5. sync_with_stdio(false)和cin.tie(NULL)

    std::ios::sync_with_stdio(false) 这个函数相当于是否兼容stdio的开关,默认为true C++为了兼容C,保证程序在使用了std::printf和std::cout的 ...

  6. ios::sync_with_stdio(false)提高C++读写速度

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:ios::sync_with_stdio(false)提高C++读写速度     本文地址:h ...

  7. CF995B Suit and Tie 贪心 第十三

    Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. sync_with_stdio

    /* The synchronization referred to is @e only that between the standard * C facilities (e.g., stdout ...

  9. std::ios::sync_with_stdio(false);

    这句语句是用来取消cin的同步,什么叫同步呢?就是iostream的缓冲跟stdio的同步.如果你已经在头文件上用了using namespace std;那么就可以去掉前面的std::了.取消后就c ...

随机推荐

  1. (Problem 46)Goldbach's other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

  2. 英文论文中“such as, for example, e.g., i.e., etc., et al. ”的用法分析 (转)

    在英文论文的编辑加工中,常会遇到such as, for example, e. g. , i. e. , etc. 和et al. 的错误及混淆使用.这里,举例分析这几个词的意义,并阐述其正确用法. ...

  3. w3c标准的selection对象介绍

    简介 术语 属性 方法 document.activeElement document.designMode = 'on'; 简介 selection是对当前激活选中区(即高亮文本)进行操作. 在非I ...

  4. A2W和W2A :很好的多字节和宽字节字符串的转换宏

    以前看<Window核心编程>,感觉多字节和宽字节之间还比较麻烦的,至少MultiByteToWideChar函数和WideCharToMultiByte函数有足够多的参数的意义让我们去理 ...

  5. BZOJ 1087 互不侵犯King (位运算)

    题解:首先,这道题可以用位运算来表示每一行的状态,同八皇后的搜索方法,然后对于限制条件不相互攻击,则只需将新加入的一行左右移动与上一行相&,若是0则互不攻击,方案可行.对于每种方案,则用递推来 ...

  6. Oracle EBS Web ADI 中的术语

    Oracle EBS Web ADI 中的术语 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 异步调用异步调用是这样子的,和引入接口表中的数 ...

  7. Java中有关构造函数的一道笔试题解析

    Java中有关构造函数的一道笔试题解析 1.详细题目例如以下 下列说法正确的有() A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与c ...

  8. leetcode第一刷_Path Sum II

    在更新上面一道题的时候我就想,是不是另一道打印路径的,果不其然啊. 这样的题非经常见的,做法也非常easy,我是用一个引用的vector来存,满足条件之后直接压入结果集中,当然也能够用数组之类的,都一 ...

  9. Json在asp.net开发中的应用

    一.asp.net后台返回Json数据,前台js解析 在后台读取数据,并手动封装成Json格式: public ContentResult getUsersByOrgId(int Id) { Data ...

  10. IIS MIME的 映射 网站有些类型的文件不能通过网页访问

    在iis中能够浏览所有扩展名的文件时,IIS MIME的 映射 您只能在故障排除过程中将通配符映射添加到 IIS MIME 映射中,以作为一种临时解决方案.确定缺少 MIME 类型是问题的原因后,请删 ...