0

I wrote the following program in C++ to measure how much time it will take to print to the default output stream in different ways:

#include <algorithm>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;

int main() {
// Get starting timepoint
auto start = high_resolution_clock::now();

for (int i=0;i<100000;i++)
{
    cout << "Hello";
}

// Get ending timepoint
auto stop = high_resolution_clock::now();

// Get duration. Substart timepoints to
// get durarion. To cast it to proper unit
// use duration cast method
auto duration = duration_cast<microseconds>(stop - start);

cout << "Time taken by function: "
     << duration.count() << " microseconds" << endl;

return 0;

}

At the first round I ran it with: cout << "Hello" << endl; and it took 147,570 microseconds.

At the second round I rant it with: cout << "Hello\n"; and took 128,543 microseconds.

Lastly, I ran it with: printf("Hello\n"); and it took 121,223 microseconds.

What caused this noticeable difference?

Note: I took the average from 10 tests for each one.

  • 2
    Also add `ios_base::sync_with_stdio(false);` and `cin.tie(NULL);` in the cout case and then compare performance to check if the numbers change. – Empty Space May 17 '20 at 15:46
  • 1
    What was the variance of the measurements? How many measurements? I other words, how do you know the difference is noticeable? – eerorika May 17 '20 at 15:53
  • I ran 10 test for each one and took the average, in total 30 tests. –  May 17 '20 at 16:07

2 Answers2

1

By default, cin/cout waste time synchronizing themselves with the C library’s stdio buffers, so that you can freely intermix calls to scanf/printf with operations on cin/cout.

Turn this off with

std::ios_base::sync_with_stdio(false);

Also many C++ tutorials tell you to write cout << endl instead of cout << '\n'. But endl is actually slower because it forces a flush, which is usually unnecessary. (You’d need to flush if you were writing, say, an interactive progress bar, but not when writing a million lines of data.) Write '\n' instead of endl.

Also as C++ is object-oriented , cin and cout are objects and hence the overall time is increased due to object binding.

So, a simple one liner, "std::ios_base::sync_with_stdio(false);" could make cin/cout faster than printf/scanf. Hope this helps you

Pranav Choudhary
  • 2,726
  • 3
  • 18
  • 38
  • now it's even faster than printf, is that normal? plus could you explain when to use endl? what do you mean by interactive progress bar –  May 17 '20 at 16:09
  • 2
    Yes, it is completely normal, also "cout << “\n”" seems performance wise better than "cout << endl;" unless flushing of stream is required. So you need to use endl when flushing is required. – Pranav Choudhary May 17 '20 at 16:42
  • Why cin/cout synchronizing themselves with the C library’s stdio buffers lead to a waste of time ? – name-1001 Dec 27 '22 at 10:03
-3

Anything in an OOP will be slower than the equivalent in a purely functional language like C. OOP comes with a price for object binding. internal syncing / flushing in is what normally slows down iostream i/o in this case, Furthur, Actual times also vary from compiler to compiler.

Using scanf() in C++ programs is faster than using cin?

look at this answer for more clarity.

Saparamadu
  • 15
  • 8