—————————————————————————————————————————————————
在函数的最前面先定义一个变量start,用clock函数记下当前的处理器使用时的时间,运行完了后再定义一个变量stop,clock一遍,用start-stop,就得出了运行时间(单位是毫秒)
#include<bits/stdc++.h>
using namespace std;
int x;
int main()
{
int start=clock();
freopen("xxx.in","r",stdin);
for(int i=1; i<=1000000; i++)
{
cin>>x;
}
int stop=clock();
cout<<endl<<stop-start;
return 0;
}
只是10^6,用了522毫秒。。。
再正常的题目中,肯定都是要运算的,这已经很危险了,暴力的话,估计只有30分。所以建议10^6的题就最好别用一般的cin了(一般的,还有不一般的,后面会说)
#include<bits/stdc++.h>
using namespace std;
int x;
int main()
{
int start=clock();
freopen("xxx.in","r",stdin);
for(int i=1; i<=1000000; i++)
{
scanf("%d",&x);
}
int stop=clock();
cout<<endl<<stop-start;
return 0;
}
用了376毫秒,已经比较安全了,但还是有隐患,如果cin暴力30分,scanf大概有45分了
#include<bits/stdc++.h>
using namespace std;
int x;
inline int read()
{
int x=0;
bool flag=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag=0;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return (flag?x:~(x-1));
}
int main()
{
int start=clock();
freopen("xxx.in","r",stdin);
for(int i=1; i<=10000000; i++)
{
x=read();
}
int stop=clock();
cout<<endl<<stop-start;
return 0;
}
!!!只有65毫秒!!! 但是我马上发现了一个致命的问题:大长了,容易写死人
ios::sync_with_stdio(false);//关闭同步流
cin.tie(0);//取消绑定
cout.tie(0);
同步流是个什么呢?
#include<bits/stdc++.h>
using namespace std;
int x;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int start=clock();
freopen("xxx.in","r",stdin);
for(int i=1; i<=1000000; i++)
{
cin>>x;
}
int stop=clock();
cout<<endl<<stop-start;
return 0;
}
用了115毫秒