7

I wanted to compare the speeds of printf and cout in C++ using this code for cout:

#include <iostream>

int main()
{
    for (int i = 0; i < 150000; i++)
        std::cout << "Hello!";
}

and this code for printf:

#include <cstdio>

int main()
{
    for (int i = 0; i < 150000; i++)
        std::printf("Hello!");
}

I ran both programs many times and this is the result (with g++ compiler):

cout: 17.116 s

printf: 9.153 s

So printf is two times faster than cout. I searched in Stack Overflow for the reasons behind this behavior and I found that printf is faster than cout because its a function while cout is an object. But I also learned that cout is slower because it's synchronized with the standard C streams.

So what I did next is to turn off synchronization of all the iostream standard streams with their corresponding standard C streams with this code:

#include <iostream>

int main()
{
    std::ios_base::sync_with_stdio(false);
    for (int i = 0; i < 150000; ++i)
        std::cout << "Hello!";
}

And surprisingly this is what I got:

printf: 9.153 s

cout with sync on: 17.116 s

cout with sync off: 1.146 s

WOW! It's a huge difference!

So my question is: would it be a good practice to always turn off the synchronization?

Thanks in advance.

L. F.
  • 19,445
  • 8
  • 48
  • 82
Toni Joe
  • 7,715
  • 12
  • 50
  • 69
  • 2
    Is printing to the standard output the main part of your program? Then yes, it's probably a good idea, – Kerrek SB Jul 20 '15 at 19:16
  • 7
    "Would it be a good practice to *always* …". No, regardless how the question ends, there is never a solution that is *always* right. – Kijewski Jul 20 '15 at 19:18
  • 1
    A few comments : such timings strongly depend on 1) what the standard output is plugged to 2) the platform 3) the standard library implementation. Also, you don't always have the choice to turn off the synchronization (it has to be done at the beginning of the program, for instance). And synchronization with C streams can also be a good feature to have, especially if you call into C libraries performing IO. – Alexandre C. Jul 20 '15 at 19:18
  • 2
    FYI if you turn off synchronization then `cout` is no longer thread safe. – NathanOliver Jul 20 '15 at 19:23
  • I think there is an argument for *synchronization* to be *off* by default. But sometimes you may want it. – Galik Jul 20 '15 at 19:29
  • 7
    *" found that print() is faster than cout because its a function while cout is an object."* I'd appreciate if you could link to the place on SO where you've read that. It sounds quite ... surprising. Also have you compiled with optimizations? Which compiler, platform? – dyp Jul 20 '15 at 19:37
  • Did you also test printf with synchronization turned off? – MikeMB Jul 20 '15 at 20:26
  • 1
    C is not C++ is not C is not Pascal! – too honest for this site Jul 20 '15 at 20:43
  • @NathanOliver It is worth pointing out that, even though `cout` is *thread safe* with synchronization, writing to `out` from multiple threads can still give you jumbled up output, just not corrupted output with UB. So, practically, if you write from multiple threads you still need to add your own level of synchronization round complete output statements. – Galik Jul 21 '15 at 09:51
  • @Galik As you mentioned about thread safe synchronization, i checked implementation,**Process Syncs** is not available as neither stream not stream_buf has mutexes. #Questioner ** IO stream sync ** depends on your usecase mainly. for example you have given it really does not affect at all. But in complex systems where you are looking for stream outputs in shell and redirecting it to other files, it does affect. for details [http://www.drdobbs.com/the-standard-librarian-iostreams-and-std/184401305] – Mahesh Attarde Jul 22 '15 at 04:14

1 Answers1

2

It Depends on if you're expected output has to be in order or not. And if you're mixing C-style or other output using the output stream. You do not want to ALWAYS turn off synchronization.

You DO NOT want to turn it off when.

  1. You are mixing Cout with other stream output functions. Like, scanf/printf, gets/puts, getchar/putchar ... ) with C++-style IO (cin/cout ... )[1]

  2. You are using threads with output that you want good output. "Concurrently accessing synchronized streams (i.e., streams for which this function returns true) never introduces data races: characters are read/written individually, although with no further guarantees on its order between threads. This may result in interleaved characters between threads unless proper synchronization of entire operations is enforced by the program."[1]

Other wise it is generally fine to Turn Off the synchronization.

also see: http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio

Harlow44
  • 102
  • 6