1

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.

Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44
  • To use codeforces ide follow the link : [link](http://codeforces.com/problemset/customtest) you might need to login to your codeforces account. – Nikhil Wagh Oct 24 '16 at 12:59
  • 5
    First off, don't do performance tests on net based compilers, they're not reliable. Now, for the first one, it's because of `endl` which forces a buffer flush. Second one is minimal difference. Anyway, a lot of detail in these functions is implementation defined and thus the library implementors might have changed the library from 11 to 14. – Hatted Rooster Oct 24 '16 at 13:03
  • 1
    `endl` flushes the `stdout` buffer. This is an expensive operation. http://stackoverflow.com/questions/4751972/endl-and-flushing-the-buffer. `\n` does not flush the buffer. – Seth Difley Oct 24 '16 at 13:03
  • @GillBates then how should I test my codes for execution time, my laptop is not a better option I believe. Please help. – Nikhil Wagh Oct 24 '16 at 13:04
  • @SethDifley That helped, thank you sir. – Nikhil Wagh Oct 24 '16 at 13:07
  • @NikhilWag: One installation only: On that particular machine. Many installations: any machine of interest, and try to analyse your data with respect to some invariant such as W/N, where W is the work =Pt and N is the number of iterations [Useful for "green computing"], or N/(tf) [iterations per cycle], or just N/t, iterations per unit of time. The latter will not take the cpu efficiency into account though: Deschutes is faster than Prescott on some operations, if the clock frequency is taken into account. This is most likely due to the fact that the latter has a horrible pipeline. – user877329 Oct 24 '16 at 13:15
  • @GillBates thanks a lot sir, I didn't understand everything :P , will google it to see explanations, thanks. . – Nikhil Wagh Oct 24 '16 at 13:20
  • You do know that you can use `'\n'` with `cout` too, right? I'm saying this only because it seems that you haven't tested `cout << i << '\n';`. – Bob__ Oct 24 '16 at 13:34
  • 1
    @NikhilWagh which compiler (version) did you use? Which standard library did you use? What optimization options did you use? Benchmarking is pointless unless optimization is enabled. – eerorika Oct 24 '16 at 13:38
  • @Bob__ oh yes, thank you for letting it to my notice. I'll try with that too. – Nikhil Wagh Oct 24 '16 at 14:10
  • @user2079303 compiler version was : GNU G++ 14 6.2.0 , library was bits/stdc++.h and I didn't use any of the optimizations. – Nikhil Wagh Oct 24 '16 at 14:21

0 Answers0