ACM输入函数测试

2017-08-27 10:26:19

writer:pprp

进行测试如下四种输入方式:

1、scanf

2、cin

3、用了ios::sync_with_stdio(false);的cin

4、自己写的输入函数(如下)

inline int read()
{
    int X=0,w=1;
    char ch=0;
    while(ch<'0' || ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
    return X*w;
}

测试先随机生成了1000以内数,保存在out.txt文件中,大概2M的内容

开始测试输入:

代码如下:

/*
@theme:ACM输入输出测试
@writer:pprp
@declare:测试scanf,优化的输入,和cin
@date:2017/8/26
*/
#include <bits/stdc++.h>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <unistd.h>

using namespace std;
const int maxn = 100000;
int a[maxn],b[maxn];

inline int read()
{
    int X=0,w=1;
    char ch=0;
    while(ch<'0' || ch>'9')
    {
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
    return X*w;
}

int main()
{
    ios::sync_with_stdio(false);
    freopen("out.txt","r",stdin);
//  freopen("out.txt","w",stdout);


    clock_t c = clock();
    for(int i = 0 ; i < maxn ; i++)
    {
//        scanf("%d%d",&a[i],&b[i]);
//        cin >> a[i] >> b[i];
//        a[i] = read();
//        b[i] = read();
    }
    clock_t d = clock();
    cout << d - c << endl;

    fclose(stdin);
    fclose(stdout);
}

测试结果是:时间:自己写的输入函数  <  有ios::sync_with_stdio(false)的cin  <  scanf   <   cin

所以在ACM输入要求严格的情况下,建议采用ios::sync_with_stdio(false)的cin

如果要求更加严格那就采用,自己写的输入函数

 

原文地址:https://www.cnblogs.com/pprp/p/7439753.html