cin真的比scanf慢吗?
qi_programmer一开始引用了此代码段,我不知道他有没有亲身测试
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cout<<"QAQ";
cin>>n;
cout<<"QWQ";
return 0;
}
emm……
接着,我复制了网上cin和scanf测试代码:(点此跳转)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
int main()
{
clock_t start, end;
char a='c';
FILE*p=freopen("C:\\Users\\Administrator\\Desktop\\out.txt", "r", stdin);
start = clock();
int i;
while (!feof(p))
{
scanf("%c", &a);
cin >> a;
}
end = clock();
::cout << end - start << endl;
::system("pause");
return 0;
}
//————————————————
//版权声明:本文为CSDN博主「Van_Le」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
//原文链接:https://blog.csdn.net/wxf2012301351/article/details/72456128
(生成测试数据代码)
#include<bits/stdc++.h>
#define f(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
int main(){
freopen("out.txt","w",stdout);
f(i,1,1000000) cout<<1<<endl;
return 0;
}
cin结果:
scanf结果:
(每次测试误差较大,这里可认为cin和scanf打平)
cout VS printf:
#include<bits/stdc++.h>
#define f(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
int main(){
f(i,1,10000)
{
//cout<<i<<endl;
printf("%d\n",i);
}
return 0;
}
cout:
printf:
欲知为何,且听下回分解