C++

「读入优化」关于读入的几个测试

测试环境: 
    操作系统: macOS High Sierra 10.13.1
    编译器: g++ 4.2.1
    CPU: 2.7 GHz Intel Core i5-5250U
//g++版本说明
➜  Desktop g++ -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

先放上测试程序代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;
int n,m,a[100000005];

int read(){
    int x=0,f=1;
    char ch;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x; 
}

void advanced_readin(){
    freopen("readinfile.in","r",stdin);
    time_t st,end;
    n=read();
    cout<<"======== advanced read in testing ======="<<endl;
    cout<<"Start with : "<<n<<" characters."<<endl;
    st=clock();
    for(int i=1;i<=n;i++){a[i]=read();}
    end=clock();
    cout<<"Testing end in : "<<double(end-st)/CLOCKS_PER_SEC<<" second."<<endl;
}

void standard_io(){
    freopen("readinfile.in","r",stdin);
    time_t st,end;
    cin>>n;
    cout<<"======== standard read in testing ======="<<endl;
    cout<<"Start with : "<<n<<" characters."<<endl;
    st=clock();
    for(int i=1;i<=n;i++){cin>>a[i];}
    end=clock();
    cout<<"Testing end in : "<<double(end-st)/CLOCKS_PER_SEC<<" second."<<endl;
}

void c_io(){
    freopen("readinfile.in","r",stdin);
    time_t st,end;
    scanf("%d",&n);
    cout<<"========   scanf read in testing  ======="<<endl;
    cout<<"Start with : "<<n<<" characters."<<endl;
    st=clock();
    for(int i=1;i<=n;i++){scanf("%d",&a[i]);}
    end=clock();
    cout<<"Testing end in : "<<double(end-st)/CLOCKS_PER_SEC<<" second."<<endl;
}

void standard_io_without_sync(){
    freopen("readinfile.in","r",stdin);
    time_t st,end;
    ios::sync_with_stdio(false);
    cin>>n;
    cout<<"======== standard read in testing ======="<<endl;
    cout<<"Start with : "<<n<<" characters."<<endl;
    st=clock();
    for(int i=1;i<=n;i++){cin>>a[i];}
    end=clock();
    cout<<"Testing end in : "<<double(end-st)/CLOCKS_PER_SEC<<" second."<<endl;
}


int main(){
    standard_io();
    c_io();
    standard_io_without_sync();
    advanced_readin();
    return 0;
}

生成数据程序代码:

#include <iostream>
using namespace std;
int main(){
    freopen("readinfile.in","w",stdout);
    int t=1000000;
    cout<<1000000<<endl;
    while(t--){
        printf("%d ",t);
    }
    return 0;
}

接下来就是读入测试:

Last login: Tue Nov  7 11:14:20 on ttys001
➜  ~ cd Desktop 
➜  Desktop ls
1             Codes         hexo          readin.cpp
1.cpp         Github        neko          readinfile.in
Code Repo     Notepad       readin
➜  Desktop ./readin

三次运行结果如下:

➜  Desktop ./readin
======== standard read in testing =======
Start with : 1000000 characters.
Testing end in : 2.87917 second.
========   scanf read in testing  =======
Start with : 1000000 characters.
Testing end in : 0.122163 second.
======== standard read in testing =======
Start with : 1000000 characters.
Testing end in : 2.9007 second.
======== advanced read in testing =======
Start with : 1000000 characters.
Testing end in : 0.370909 second.

➜  Desktop ./readin
======== standard read in testing =======
Start with : 1000000 characters.
Testing end in : 2.90104 second.
========   scanf read in testing  =======
Start with : 1000000 characters.
Testing end in : 0.122007 second.
======== standard read in testing =======
Start with : 1000000 characters.
Testing end in : 2.887 second.
======== advanced read in testing =======
Start with : 1000000 characters.
Testing end in : 0.372498 second.

➜  Desktop ./readin
======== standard read in testing =======
Start with : 1000000 characters.
Testing end in : 2.90907 second.
========   scanf read in testing  =======
Start with : 1000000 characters.
Testing end in : 0.122465 second.
======== standard read in testing =======
Start with : 1000000 characters.
Testing end in : 2.91914 second.
======== advanced read in testing =======
Start with : 1000000 characters.
Testing end in : 0.371009 second.

说明:

可能是因为我的关同步写的不对,测试的时间和cin读入是一样的。scanf竟然比read()更快一些。

可能因为风水的原因,读入优化没那么快。

分享到