Though there is no difference between the working of endl and \n. Both work as a ‘enter Key’. But there is a huge difference between their working mechanism. Endl flushes the output buffer and ‘\n’ doesn’t. if you want the buffer flushed frequently, use ‘\n’. if you do, use endl.
Endl is actually slower because it forces a flush, which actually unnecessary. You would need to force a flush right before prompting the user for input from cin, but not when writing a million lines of output. Write ‘\n ’ instead of endl. **** The only difference is that std::endl flushes the output buffer, and '\n' doesn't. If you don't want the buffer flushed frequently, use '\n'. If you do (for example, if you want to get all the output, and the program is unstable), use std::endl.
The difference can be illustrated by the following: std::cout << std::endl; is equivalent to std::cout << '\n' << std::flush; So, • Use std::endl If you want to force an immediate flush to the output. if you are in a command line app and want to guarantee that the user can see the output immediately. • Use \n if you are in a worried about performance (which is probably not the case if you are using the << operator). I use \n on most lines. Then use std::endl at the end of a paragraph (but that is habit it is usually not necessary). Contrary to other claims, the \n character is mapped to the correct platform end of line sequence only if the stream is going to a file (std::cin and std::cout being special but still files (or file-like)).
Yet another reason why C++ sucks?
In the C's stdlib a
FILE*
can have three bufferization modes:fwrite
'n to theFILE*
fwrite
By default when the
FILE*
points to a terminal it is line-buffered, when it points to a file it is fully-buffered. Sounds reasonable, right? Because every flush does a system call (at least on Linux) that requires switching to Kernel mode and back, messing with filesystem caches, holding locks when necessary, etc. So why would one want to get all that overhead and flush it on every newline when printing to a file?I've run a program that does
cout << "a" << endl;
106 times with stdout redirected to a file. Time measured bytime
was:strace
has shown, that data is flushed for each two bytes.Then I changed
endl
to"\n"
, and the result was:And finally using
printf
in pure C:The last two examples were flushing by 4096 bytes at a time.
std::cout
wastes a lot of time trying to staying in sync with C-style output. If your program only usesstd::cin
andstd::cout
without using C-style output, turning the sync off withstd::ios::sync_with_stdio(false)
will offer a reasonable speedup.Here are the same tests with
ios::sync_with_stdio(false)
done right before the loop in first two examples in the same order:It is not so much different from previous one, is it?
Look at the last two examples, the
sys
fields are exactly the same, the noticeable difference is inuser
fields. That is the time program spent executing its own code. This is the overhead brought by the waycout
is implemented.Now, look at the first example, most of the time is spent in kernel mode(
sys
) executing syscalls. You could write your own IO functions in C or even assembly and reduce theuser
field, but you won't get rid of overhead insys
field if you still make a syscall for every two characters.I was talking about how bad it is to flush very often, not about how slow the C++ and its
cout
is.You would need to force a flush right before prompting the user for input from cin, but not when writing a million lines of output.
Don't know about C++, but in the C world line-buffered FILEs are flushed before reading something from a FILe attached to a terminal(like stdin in most cases). Cool, isn't it?
What are you talking about? cout is a stream, not a file and in C++ you have everything from C plus some other things. I don't see why C++ sucks while C doesn't.
I'm not sure what you mean by saying 'file'. In C there is
FILE*
that allows you tofwrite
/fread
to/from files on disk, sockets, terminals, pipes, etc. Also you can specifyFILE*
's flushing behavior. That is what I was talking about.On the other hand in C++ there is
cout
that is an instance of classostream
that allows similar operations. I don't know if thecout
is implemented on top ofFILE*
or if it directly calls platform-dependent IO functions(syscalls etc).The C++ way of doing thing things sucks. Flushing data after each newline when writing to a file is like cutting off your balls with a chainsaw. I would better shoot myself in a foot in C.
Of course you could use good old cstdlib functions from your C++ code and almost everyting you have in C. But that wouldn't be C++ way I was complaining about.