lundi 24 octobre 2016

Why is cin and cout faster in c++14?

How did they make cin and cout work faster in C++ 14. I also want to know what is the effect of endl and \n, they affected the time of execution. I tested these codes on codeforces ide, and got following results. c++ 14 cout with endl:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i=0;
    for(i=0;i<1000000;i++)
    cout<<i<<endl;
}

this took 1699ms while,

c++ 14 cout without endl

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i=0;
    for(i=0;i<1000000;i++)
    cout<<i;
}

this took 109ms.

c++ 14 printf without \n

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i=0;
    for(i=0;i<1000000;i++)
    printf("%d",i);
}

this took 171ms.

c++ 14 with \n

#include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int i=0;
        for(i=0;i<1000000;i++)
        printf("%d\n",i);
    }

this took 186ms.

I'm not pasting the codes now.

C++ 11 cout without endl took 327ms.

c++ 11 cout with endl took 2245ms.

c++ 11 printf without \n took 186ms.

c++ 11 printf with \n took 218ms.

C++ 14 surely is faster I want to know what they did with cin and cout, and why is endl increasing execution time. Thank You.

Aucun commentaire:

Enregistrer un commentaire