Cin, cout, printf, scanf efficiency comparison, cinscanf
From: http://www.cnblogs.com/killerlegend/p/3918452.html
Author: KillerLegend
Date: 2014.8.17
Hangdian OJ 3233 is a very simple problem. Who knows that I have always been prompted to time out, time out, and finally almost cried? The C ++ code is as follows:
#include <iostream>#include <iomanip>using namespace std;int main(){int t,tmp,band,index=0;while(cin>>t>>tmp>>band){if(t==0||tmp==0||band==0) break;double a,b,sum=0.0;for(int i=0;i<t;++i){cin>>a>>b;sum+=a*(100-b)*0.01;}cout<<"Case "<<++index<<": "<<fixed<<setprecision(2)<<sum/band<<endl<<endl;}return 0;}
Later, I switched the input and output streams to scanf and printf, and the result was amazing...
#include <cstdio>int main(){int t,tmp,band,index=0;while(scanf("%d%d%d",&t,&tmp,&band)){if(t==0||tmp==0||band==0) break;double a,b,sum=0.0;for(int i=0;i<t;++i){scanf("%lf%lf",&a,&b);sum+=a*(100-b)*0.01;}printf("Case %d: %.2f\n\n",++index,sum/band);}return 0;}
This is really sad... so I'm going to test cin, scanf, cout, and printf:
Cout test:
#include <iostream>#include <cstdio>#include <time.h>using namespace std;int main(){freopen("in.conf","r",stdin);freopen("out.conf","w",stdout);clock_t t1,t2,t3,t4;t1 = clock();for(int i=0;i<10000000;++i)cout<<i<<" ";t2 = clock();cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;return 0;}
Result: 5.206 s
Printf test:
#include <iostream>#include <cstdio>#include <time.h>using namespace std;int main(){freopen("in.conf","r",stdin);freopen("out.conf","w",stdout);clock_t t1,t2,t3,t4;t1 = clock();for(int i=0;i<10000000;++i)printf("%d ",i);t2 = clock();cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;return 0;}
Result: 6.202 s
We use the data generated by the previous loop of 10 million times (78.89 MB) for the test data during the following reading:
Read using cin:
#include <iostream>#include <cstdio>#include <time.h>using namespace std;int main(){freopen("out.conf","r",stdin);freopen("in.conf","w",stdout);clock_t t1,t2,t3,t4;t1 = clock();int k;for(int i=0;i<10000000;++i)cin>>k;t2 = clock();cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;return 0;}
The result is 38.507 s.
Use scanf to read:
#include <iostream>#include <cstdio>#include <time.h>using namespace std;int main(){freopen("out.conf","r",stdin);freopen("in.conf","w",stdout);clock_t t1,t2,t3,t4;t1 = clock();int k;for(int i=0;i<10000000;++i)scanf("%d",&k);t2 = clock();cout<<"cin-time:"<<(double)(t2-t1)/CLOCKS_PER_SEC<<endl;return 0;}
The result is 4.204 s.
Result List:
Conclusion: Try to use cout for output and scanf for input.
Cin cout efficiency is not as high as scanf printf
There are mainly the following reasons:
1. Stream Input and Output are easy to use for basic types, and do not need to control strings by hand.
2. For some classes in the standard library, it is obvious that the overload operator is much easier to control strings than writing their own formats.
3. You can use custom operators for complex formats.
4. better readability (many people have different opinions and opinions ).
In principle, stream operations are more efficient than printf/scanf Function Families, because they determine the operand type and the called output function during compilation, you do not need to control the extra overhead of strings in the parsing format at runtime. However, for the sake of universality, the inheritance system of the standard Stream object cin/cout is very complex. Therefore, the object construction will affect the efficiency, so the overall efficiency is relatively low. If optimization is performed based on specific scenarios, the efficiency can be improved.
====
[Original reply Group]
References: original
In C ++, is cout less efficient than scanf and printf?
Correct. If you are doing io or acm, try not to use cin or cout. If you try to print 99999999, you will know that the speed difference between them is about 10 times.