“endl” vs “\n”, Why you should avoid ‘endl’?

Both endl and \n are almost same with a slight difference which has a huge impact on programs. Till now you have written many program in C++ ending the ‘cout’ statements happily with endl to print newlines. To this day I too believed, that endl is better that \n in C++. But It turns out that we’re following the wrong rule.

And the fact is endl not only add newlines to the I/O stream, but after sending the cursor to the next line it flushes the buffer each time used. For small programs, the difference is un-noticeable. For bulk of I/O operations the efficiency of the program decreases.

When we write cout << std::endl

We are actually doing something like this cout << '\n' << std::flush;

Both are pretty much same, one with extra flush.

endl always flushes the stream buffer. Whereas, \n simply puts a new line character to the stream without flushing any data which decreases efficiency. If needed, we can call std::flush, afterwards explicitly using either cout.flush() or by inserting std::flush into the stream.

The operating system is responsible for flushing of buffers. Each time program requests for flushing, the operating system will do it. But this request is comparatively expensive. So we don’t need to flush buffers every time we create a new line. The IO streams automatically flush the buffer when it is full.

// endl vs \n Example 1
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
#include <thread>
 
int main()
{
    std::clock_t c_start = std::clock();
    auto t_start = std::chrono::high_resolution_clock::now();
    {
        for(int i=0;i<100;i++)
            std::cout<<"Hello"<<std::endl;
    }
    std::clock_t c_end = std::clock();
    auto t_end = std::chrono::high_resolution_clock::now();
 
    std::cout << std::fixed << std::setprecision(2) << "CPU time used: "
              << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"
              << "Wall clock time passed: "
              << std::chrono::duration<double, std::milli>(t_end-t_start).count()
              << " ms\n";
}

Output will be 100 times Hello than

CPU time used: 0.51 ms
Wall clock time passed: 0.50 ms
// endl vs \n Example 2
#include <iostream>
#include <iomanip>
#include <chrono>
#include <ctime>
#include <thread>
 
int main()
{
    std::clock_t c_start = std::clock();
    auto t_start = std::chrono::high_resolution_clock::now();
    {
        for(int i=0;i<100;i++)
            std::cout<<"Hello"<<"\n";
    }
    std::clock_t c_end = std::clock();
    auto t_end = std::chrono::high_resolution_clock::now();
 
    std::cout << std::fixed << std::setprecision(2) << "CPU time used: "
              << 1000.0 * (c_end-c_start) / CLOCKS_PER_SEC << " ms\n"
              << "Wall clock time passed: "
              << std::chrono::duration<double, std::milli>(t_end-t_start).count()
              << " ms\n";
}

Output will be 100 times Hello than

CPU time used: 0.11 ms
Wall clock time passed: 0.10 ms

If codeblock not working use online compiler here: ONLINE IDE

Run both the above codes you will know what std::flush do.

As you can see the time required 100 lines of texts by using std::endl, takes more times to complete the task compared to using ‘\n’ after it.

These two endl and \n doing the same task with a slight difference. endl equivalent to ‘\n’ but slow because doing redundant flush(). If performance is not the priority, we can use endl freely and also note that cin/cout performs flush automatically which is a topic for another article.

Writing ‘\n’ characters directly to the stream is more efficient since it doesn’t force a flush like endl.

As seen from the output std::endl took nearly more time than \n. On some systems, the performance impact could be even worse and note that it does guaranteed that std::endl will take more time than printing ‘\n’ directly to the stream.

Some other differences between endline and \n are:

  • \n is character, endl is manipulator.
  • \n requires 1 byte of memory whereas endl doesn’t.
  • We can use \n also in C but we can’t use endline in C language.

Leave a Reply

Your email address will not be published. Required fields are marked *