The difference between endl and '\ n'

Possible duplicate:
C ++: "std :: endl" vs "\ n"

I have a simple program that I tested, and I understand that endl is detrimental to my program. Using endl, my program worked at a speed of 100+ ms when working with '\n' , the time decreased to ~ 50 ms . Can anyone say why there is such a difference?

PS I read other posts that somehow explained what each of them does, but does std::flush really take so long?
Or maybe there is another possible explanation?

+4
source share
2 answers

endl has an extra expensive flush() operation

27.7.3.8 Standard Basic_ostream Manipulators [ostream.manip]

 namespace std { template <class charT, class traits> basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os); } 1 Effects: Calls os.put(os.widen('\n')), then os.flush(). 2 Returns: os. 
+2
source

std::endl writes a new line, and flushes the buffer . As you discovered, a flash can be quite an expensive operation.

+6
source

All Articles