0

I'm working on a program that prints different statements. In the following code, which case would be faster?

Case 1:

    cout << "----------------------------------" << endl;
    cout << "Email is not found in the database" << endl;
    cout << "----------------------------------\n" << endl;

Case 2:

    cout << "----------------------------------\nEmail is not found in the database\n----------------------------------\n" << endl;

Thanks in advance.

  • 2
    Have you tried both (at varying optimization levels) and checked to see how much time they take? – Chris Dec 31 '21 at 23:24
  • Possibly useful: https://stackoverflow.com/questions/213907/stdendl-vs-n – Chris Dec 31 '21 at 23:25
  • 1
    Since `endl` does more than ending a line, I'm confident it's the second one. Will it be measurable? Writing this question might have taken more time than the optimized code will ever save. – Quimby Dec 31 '21 at 23:28
  • 1
    @Quimby -- if standard in has been redirected to a file, `'\n'` will be much faster than `std::endl`. `std::endl` should only be used when it's needed. – Pete Becker Dec 31 '21 at 23:31
  • @PeteBecker `stdout`, even. – Paul Sanders Jan 01 '22 at 00:54
  • @PaulSanders — whoops, that’s what I meant. :-( – Pete Becker Jan 01 '22 at 01:27
  • Don't worry, it happens to the best of us @Pete. Anyway, you made a good point there, so many people don't seem to understand the difference. – Paul Sanders Jan 01 '22 at 07:53

0 Answers0