cout speed when synchronization is off

后端 未结 1 467
北荒
北荒 2021-01-01 03:20

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

#include 

int main         


        
相关标签:
1条回答
  • 2021-01-01 03:41

    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

    0 讨论(0)
提交回复
热议问题