提高C++代码输入输出执行效率
#include <iostream>
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr)
/*代码的头文件是 iostream
就不要用scanf ,getchar,gets,fgets,fscanf了。
好处:将cin cout速度提升至与scanf相差无几。
副作用:不能scanf cin之类的混用了。*/
}
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// std::ios::sync_with_stdio(false);
// std::cin.tie(0),cout.tie(0);
原来而cin,cout之所以效率低,是因为先把要输出的东西
存入缓冲区,再输出,导致效率降低,
而这段代码可以来打消iostream的输入 输出缓存,
可以节省许多时间,使效率与scanf与printf相差无几.
说白了,这几句加上就可以提高C++代码输入输出执行效率
使得和C相差无几
计算程序执行时间
// 计算程序执行时间
#include <ctime>
int main()
{
int start = clock();
//DO SOMETHING
printf( "%.3lf\n", double( clock()-start ) / CLOCKS_PER_SEC );
}