• [亲测]输入方式比较


    前言

    之前一直有用cin  cout的习惯,因为不用管繁杂的格式。但是之前集训时有一次一个题超时了,怎么改也改不对,这时才听说cin比scanf耗时,改成scanf过了。从此再也不敢用cin了。直到后来又听说ios::sync_with_stdio(false)可以关闭iostream和cstdio的同步以优化cin,后来又听说还有一种叫快读的东西……那么这么多输入方式到底哪个快?今天亲测一下。

    声明:本人使用的是DEV C++进行的测试。Anyixing同学反映他的测试结果和本文所记录的不一样,经查验发现是编译器不一样导致的。所以本文仅适用于DEV C++中跑的程序,仅供参考

    测试

    字符串+int+字符混合读入

    这部分涉及字符和字符串,没法快读,只比较scanf,cin以及优化cin。

    测试数据:1000000个“abcdefg”,1000000个数字10000,1000000个‘c’。

    代码展示,以示公正。

     1 /*scanf*/
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 clock_t start,end;
     5 int main(){
     6     freopen("in.in","r",stdin);
     7     freopen("scanf.out","w",stdout);
     8     char s[7],c;int a;
     9     start=clock();    
    10     for(int i=1;i<=1000000;i++) scanf("%s",s);
    11     for(int i=1;i<=1000000;i++) scanf("%d",&a);
    12     for(int i=1;i<=1000000;i++) scanf("%c",&c);
    13     end=clock();
    14     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    15     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    16     return 0;
    17 } 
    scanf
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 clock_t start,end;
     4 int main(){
     5     freopen("in.in","r",stdin);
     6     freopen("cin.out","w",stdout);
     7     char s[7],c;int a;
     8     start=clock();    
     9     for(int i=1;i<=1000000;i++) cin>>s;
    10     for(int i=1;i<=1000000;i++) cin>>a;
    11     for(int i=1;i<=1000000;i++) cin>>c;
    12     end=clock();
    13     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    14     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    15     return 0;
    16 } 
    cin
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 clock_t start,end;
     4 int main(){
     5     ios::sync_with_stdio(false);
     6     freopen("in.in","r",stdin);
     7     freopen("优化cin.out","w",stdout);
     8     char s[7],c;int a;
     9     start=clock();    
    10     for(int i=1;i<=1000000;i++) cin>>s;
    11     for(int i=1;i<=1000000;i++) cin>>a;
    12     for(int i=1;i<=1000000;i++) cin>>c;
    13     end=clock();
    14     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    15     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    16     return 0;
    17 } 
    优化cin

    测试结果:

    scanf:Total time:2798ms

    cin:Total time:4004ms

    优化cin:Total time:892ms

    第一:优化cin

    第二:scanf

    第三:cin

    所以,这是优化cin的大胜利!

    可以清晰地看出单纯的cin是真的慢,而优化的cin比scanf还快不少,差距悬殊。

    仅int读入

    仅int读入,快读加入战斗。

    测试数据:5000000个数字10000 。

    代码展示,以示公正。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 clock_t start,end;
     4 int main(){
     5     freopen("in.in","r",stdin);
     6     freopen("scanf.out","w",stdout);
     7     int a;
     8     start=clock();    
     9     for(int i=1;i<=5000000;i++) scanf("%d",&a);
    10     end=clock();
    11     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    12     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    13     return 0;
    14 } 
    scanf
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 clock_t start,end;
     4 int main(){
     5     freopen("in.in","r",stdin);
     6     freopen("cin.out","w",stdout);
     7     int a;
     8     start=clock();    
     9     for(int i=1;i<=5000000;i++) cin>>a;
    10     end=clock();
    11     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    12     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    13     return 0;
    14 } 
    cin
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 clock_t start,end;
     4 int main(){
     5     ios::sync_with_stdio(false);
     6     freopen("in.in","r",stdin);
     7     freopen("优化cin.out","w",stdout);
     8     int a;
     9     start=clock();    
    10     for(int i=1;i<=5000000;i++) cin>>a;
    11     end=clock();
    12     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    13     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    14     return 0;
    15 } 
    优化cin
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 inline int read(){
     4    int s=0,w=1;
     5    char ch=getchar();
     6    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
     7    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
     8    return s*w;
     9 } 
    10 clock_t start,end;
    11 int main(){
    12     ios::sync_with_stdio(false);
    13     freopen("in.in","r",stdin);
    14     freopen("快读.out","w",stdout);
    15     int a;
    16     start=clock();    
    17     for(int i=1;i<=5000000;i++) a=read();
    18     end=clock();
    19     double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    20     cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    21     return 0;
    22 } 
    快读

    测试结果:

    scanf:Total time:6632ms

    cin:Total time:8627ms

    优化cin:Total time:1748ms

    快读:Total time:2256ms

    第一:优化cin

    第二:快读

    第三:scanf

    第四:cin

    又是优化cin的大胜利!

    可见优化cin和快读的时间相差不多,正常的小数据应该看不出什么区别,但这两个甩了scanf和普通cin好多。

    结语

    两次都是优化cin夺得第一。但是有几个小提示:

    1.cin确实方便,但输出小数有位数要求的时候还是用scanf吧

    2.使用优化cin的时候绝对不能再用scanf和printf,编译能过,但是不管输入什么都直接RE

    幸甚至哉,歌以咏志

  • 相关阅读:
    linux socket里的send和recv,阻塞与非阻塞socket、TCP与UDP在这方面的区别
    leetcode 149 Max Points on a Line
    leetcode 126 Word Ladder II
    leetcode 123 Best Time to Buy and Sell Stock III
    LC 297 Serialize and Deserialize Binary Tree
    栈和队列问题
    链表问题
    day17--权限管理和配置服务
    谷粒学院功能简介及系统架构
    day01--MybatisPlus的使用
  • 原文地址:https://www.cnblogs.com/DarthVictor/p/12617949.html
Copyright © 2020-2023  润新知