Optimization of input and output streams

As we all know, because of some indescribable, cin input efficiency is much lower than the scanf. So, we enrolled out in order to speed read, found some strange read the board.

Cipian only used as a template speed, not to discuss the principle and controversy.

Close synchronous flow 

cin optimized for. It would be able to reach speeds scanf level.

    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

 

Read function (short)

The drawback is that for integers seemingly can not read long long (I remember not)

Although fast and efficient and sacnf, but not the fastest one.

void Read(int &x)
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

I noticed when opening iostream header file, dev is given as a function of the prompt read. In other words, read unsure whether a conflict.

For insurance purposes, we will read their own written into the function capitalized. That Read ()

Read function bis (length)

This is my first experience to the fastest read method. And he supports the long long type.

But the obvious drawbacks, very long, but the two functions also comes with a not very low array.

Be careful when using a memory card case. It can be appropriately reduced array size

const int MAXSIZE=1<<12;
inline char gc()
{
    static char In[MAXSIZE], *at = In, *en = In;
    if (at == en)
    {
        en = (at = In) + fread(In, 1, MAXSIZE, stdin);
    }
    return at == en ? EOF : *at++;
}
inline long long Read()
{
    char c;
    while (c = gc(), !(c >= '0'&&c <= '9') && c != '-') {}
    bool f = c == '-';
    long long x = f ? 0 : c - '0';
    for (c = gc(); c >= '0'&&c <= '9'; c = gc())
    {
        x = x * 10 + c - '0';
    }
    return f ? -x : x;
}

 

Guess you like

Origin www.cnblogs.com/Uninstalllingyi/p/11579469.html