Input and output hang test template

Foreword

Playing newcode reproduce and Shanghai race when the two encountered a very similar time-out situation, in particular the proper way the question suggests special "Please use a more efficient input mode", misled me for a long time ,,,,
well-known standardized input and output ( scanf, printf) faster than the input and output stream (cin, cout), the reason can be found in a lot of Bowen, where the election a
https://www.cnblogs.com/xienaoban/p/6798095.html

Faster is linked to the input and output, because getchar () faster than Scanf,
so with the following formula:
input speed: getchar> scanf> cin

To test

First, randomly generated one million integers, write to the specified file

#include <ctime>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    srand(time(NULL));
    freopen("00out.txt", "w", stdout);
    int n;
    int num = 1000000;
    while(num--)
    {
        n = rand() % 1000;
        cout << n << ' ';
    }
    return 0;
}

Then, each linked to the input (getchar), scanf, cin read one million integer numbers, see the runtime

#include <ctime>
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1000005;
int a[N];
int b[N];

int Scan()     ///输入外挂
{
    int res = 0, ch ,flag = 0;
    if( (ch = getchar()) == '-' )
        flag = 1;
    else if(ch >= '0' && ch <= '9')
        res = ch - '0';
    while((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + ch - '0';
    return flag ? -res : res;
}
void Out(int a)    ///输出外挂
{
    if(a < 0)
    {
        putchar('-');
        a *= -1;
    }
    if(a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

int main()
{
    clock_t start, over;
    double  duration;

    freopen("00out.txt", "r", stdin);
    start = clock();
    for(int i = 0; i < 1000000; ++i)
        a[i] = Scan();
    over = clock();
    duration = double(over - start) / CLOCKS_PER_SEC;
    cout << duration << " (s)" << '\n';
    fclose(stdin);

    freopen("00out.txt", "r", stdin);
    start = clock();
    for(int i = 0; i < 1000000; ++i)
        scanf("%d", &a[i]);
    over = clock();
    duration = double(over - start) / CLOCKS_PER_SEC;
    cout << duration << " (s)" << '\n';
    fclose(stdin);

    freopen("00out.txt", "r", stdin);
    start = clock();
    for(int i = 0; i < 1000000; ++i)
        cin >> a[i];
    over = clock();
    duration = double(over - start) / CLOCKS_PER_SEC;
    cout << duration << " (s)" << '\n';
    fclose(stdin);
}

Then

Here Insert Picture Description

Ah ,,, so enter quickly hung (but rarely used, with this card really happened much the past)

O hang template

int Scan()     ///输入外挂
{
    int res = 0, ch ,flag = 0;
    if( (ch = getchar()) == '-' )
        flag = 1;
    else if(ch >= '0' && ch <= '9')
        res = ch - '0';
    while((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + ch - '0';
    return flag ? -res : res;
}
void Out(int a)    ///输出外挂
{
    if(a < 0)
    {
        putchar('-');
        a *= -1;
    }
    if(a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

Read into the double hung on (but less than)

#include <cstdio>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

double Scan()
{
    int ch, pos = 1, num = 0;///num为字符串总长度,pos为小数点排第几位
    double res = 0;
    bool flag = 0, found = 0;

    if( (ch = getchar()) == '-' )
        flag = 1;
    else if(ch >= '0' && ch <= '9')
        res = ch - '0';
    num++;

    while((ch = getchar()) >= '0' && ch <= '9' || ch == '.')
        {
            num++;
            if(ch == '.')
            {
                found = 1;
                pos++;
            }
            else
            {
                res = res * 10 + ch - '0';
                if(!found)
                    pos++;
            }
        }
    res *= pow(0.1, num - pos);
    return flag ? -res  : res ;
}

int main()
{
    double ans;
    while(ans = Scan())
    {
        cout << fixed << std::setprecision(10) << ans << '\n';
    }
    return 0;
}

Precautions

scanf("%d", &n);
getchar();
for(int i = 0; i < n; ++i)
    a[i] = Scan();

Getchar middle of the surface did not affect the time () may not be combined with the output of the test, but do question will give you off WA

Published 11 original articles · won praise 2 · Views 3997

Guess you like

Origin blog.csdn.net/qq_39657434/article/details/104364452