{"id":5736,"date":"2019-07-04T16:50:04","date_gmt":"2019-07-04T16:50:04","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-improved-performance-with-iostreams\/"},"modified":"2023-06-26T10:05:30","modified_gmt":"2023-06-26T10:05:30","slug":"c-core-guidelines-improved-performance-with-iostreams","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-improved-performance-with-iostreams\/","title":{"rendered":"C++ Core Guidelines: Improved Performance with Iostreams"},"content":{"rendered":"<p>As easy as my title and the rules of the C++ core guidelines sound, getting more performance out of the iostreams is no no-brainer.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5725\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sport-659224_1280.jpg\" alt=\"sport 659224 1280\" width=\"500\" height=\"294\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sport-659224_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sport-659224_1280-300x176.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sport-659224_1280-1024x602.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/sport-659224_1280-768x451.jpg 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>Okay, let&#8217;s step back. Although I did a lot of tests,&nbsp; my numbers in this post are more controversial than I thought. Please let me know if you have any ideas, improvements, or clarifications, and I will add them to this post.<\/p>\n<p>Here are the two performance-related rules from the guidelines to Iostreams.<\/p>\n<ul>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-sync\">SL.io.10: Unless you use <code class=\"highlighter-rouge no-highlight\">printf<\/code>-family functions call <code class=\"highlighter-rouge no-highlight\">ios_base::sync_with_stdio(false)<\/code><\/a><\/li>\n<li><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-endl\">SL.io.50: Avoid <code class=\"highlighter-rouge no-highlight\">endl<\/code><\/a><\/li>\n<\/ul>\n<p>I assume you don&#8217;t know <span style=\"font-family: courier new, courier;\">std::ios_base::sync_with_stdio<\/span>?<\/p>\n<h2><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-sync\">SL.io.10: Unless you use <code class=\"highlighter-rouge no-highlight\">printf<\/code>-family functions call <code class=\"highlighter-rouge no-highlight\">ios_base::sync_with_stdio(false)<\/code><\/a><\/h2>\n<p>Per default, operations on the C++ streams are synchronized with the C streams. This synchronization happens after each in- or output operation.<\/p>\n<ul>\n<li>C++ Streams: <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/cin\" title=\"cpp\/io\/cin\">std::cin<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/cout\" title=\"cpp\/io\/cout\">std::cout<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/cerr\" title=\"cpp\/io\/cerr\">std::cerr<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/clog\" title=\"cpp\/io\/clog\">std::clog<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/cin\" title=\"cpp\/io\/cin\">std::wcin<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/cout\" title=\"cpp\/io\/cout\">std::wcout<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/cerr\" title=\"cpp\/io\/cerr\">std::wcerr<\/a><\/span>, and <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/clog\" title=\"cpp\/io\/clog\">std::wclog<\/a><\/span>.<\/li>\n<li>C Streams: <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/c\" title=\"cpp\/io\/c\">stdin<\/a><\/span>, <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/c\" title=\"cpp\/io\/c\">stdout<\/a><\/span>, and <span class=\"t-lc\"><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/io\/c\" title=\"cpp\/io\/c\">stderr<\/a><\/span>.<\/li>\n<\/ul>\n<p>This allows it to mix C++ and C in- or output operations because operations on the C++ streams go unbuffered to the C streams. What is also important to note from the concurrency perspective: synchronized C++ streams are thread-safe. All threads can write to the C++ streams without any need for synchronization. The effect may be an interleaving of characters but not a data race.<\/p>\n<p>When you set the<span style=\"font-family: courier new, courier;\"> std::ios_base::sync_with_stdio(false)<\/span>, the synchronization between C++ streams and C streams will not happen because the C++ stream may put its output into a buffer. Because of the buffering, the in- and output operation may become faster. You must invoke<span style=\"font-family: courier new, courier;\"> std::ios_base::sync_with_stdio(false)<\/span> before any in- or output operation. If not, the behavior is implementation-defined.<\/p>\n<p>I assume you noticed that I wrote quite often, maybe. That is for a reason.<\/p>\n<h3>Interleaving of C++ Streams and C Streams<\/h3>\n<p>First,&nbsp; I want to know what would happen when I execute the following program with various compilers.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ syncWithStdio.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;cstdio&gt;<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>ios<span style=\"color: #555555;\">::<\/span>sync_with_stdio(<span style=\"color: #336666;\">false<\/span>);\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"1\"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>printf(<span style=\"color: #cc3300;\">\"2\"<\/span>);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"3\"<\/span>;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I add a few pieces of information to get a better picture of my various compiler.<\/p>\n<h4>GCC 8.2<\/h4>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4858\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/08\/gcc.png\" alt=\"gcc\" width=\"600\" height=\"180\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5726\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioLinux.png\" alt=\"SyncWithStdioLinux\" width=\"400\" height=\"152\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioLinux.png 836w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioLinux-300x114.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioLinux-768x291.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<h4>Clang 8.0<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5727\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clang.PNG\" alt=\"clang\" width=\"400\" height=\"167\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clang.PNG 1434w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clang-300x125.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clang-1024x428.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clang-768x321.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5728\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioClang.png\" alt=\"SyncWithStdioClang\" width=\"400\" height=\"156\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioClang.png 1151w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioClang-300x117.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioClang-1024x400.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioClang-768x300.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h4>cl.exe 19.20<\/h4>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5729\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe.PNG\" alt=\"clexe\" width=\"600\" height=\"236\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe.PNG 2330w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe-300x118.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe-1024x404.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe-768x303.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe-1536x606.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/clexe-2048x809.png 2048w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5730\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioWin.png\" alt=\"SyncWithStdioWin\" width=\"400\" height=\"145\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioWin.png 1244w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioWin-300x109.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioWin-1024x370.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/SyncWithStdioWin-768x278.png 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>It seems that only the output on GCC is not synchronized. This observation does not hold for clang or cl.exe on Windows. A small performance test confirmed my first impression.<\/p>\n<\/p>\n<h3>Performance with and without Synchronisation<\/h3>\n<p>Let me write a small program with and without synchronization to the console. Doing it without synchronization should be faster.<\/p>\n<ul>\n<li><strong>Synchronized<\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ syncWithStdioPerformanceSync.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;fstream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;random&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;sstream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">int<\/span> iterations <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">10<\/span>;\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>ifstream openFile(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> myFile){                  \r\n\r\n  std<span style=\"color: #555555;\">::<\/span>ifstream file(myFile, std<span style=\"color: #555555;\">::<\/span>ios<span style=\"color: #555555;\">::<\/span>in);\r\n  <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( <span style=\"color: #555555;\">!<\/span>file ){\r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Can't open file \"<\/span><span style=\"color: #555555;\">+<\/span> myFile <span style=\"color: #555555;\">+<\/span> <span style=\"color: #cc3300;\">\"!\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    exit(EXIT_FAILURE);\r\n  }\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> file;\r\n  \r\n}\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>string readFile(std<span style=\"color: #555555;\">::<\/span>ifstream file){                        \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>stringstream buffer;\r\n    buffer <span style=\"color: #555555;\">&lt;&lt;<\/span> file.rdbuf();\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> buffer.str();\r\n    \r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> writeToConsole(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> fileContent){\r\n     \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> c<span style=\"color: #555555;\">:<\/span> fileContent) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> c;\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> dur <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now() <span style=\"color: #555555;\">-<\/span> start;\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> dur;\r\n}  \r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Function<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> measureTime(std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> iter, Function<span style=\"color: #555555;\">&amp;&amp;<\/span> f){\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> dur{};\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> iter; <span style=\"color: #555555;\">++<\/span>i){\r\n        dur <span style=\"color: #555555;\">+=<\/span> f();\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> dur <span style=\"color: #555555;\">\/<\/span> iter;\r\n}\r\n    \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(<span style=\"color: #007788; font-weight: bold;\">int<\/span> argc, <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> argv[]){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ get the filename<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>string myFile;\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( argc <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">2<\/span> ){\r\n        myFile<span style=\"color: #555555;\">=<\/span> argv[<span style=\"color: #ff6600;\">1<\/span>];\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n        std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Filename missing !\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n        exit(EXIT_FAILURE);\r\n    } \r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>ifstream file <span style=\"color: #555555;\">=<\/span> openFile(myFile);                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>string fileContent <span style=\"color: #555555;\">=<\/span> readFile(std<span style=\"color: #555555;\">::<\/span>move(file));                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<br \/> <\/span>\r\n                                                                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>   \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> averageWithSync <span style=\"color: #555555;\">=<\/span> measureTime(iterations, [<span style=\"color: #555555;\">&amp;<\/span>fileContent]{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> writeToConsole(fileContent); });\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n                                                                            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span> \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"With Synchronisation: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> averageWithSync.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" seconds\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The program is relatively easy to explain. I open a file (line 1), read its entire content (line 2) into a string, and write its <span style=\"font-family: courier new, courier;\">iterations<\/span>-times to the console (line 3). This is done in the function <span style=\"font-family: courier new, courier;\">writeToConsole(fileContent)<\/span>.<\/p>\n<p>iterations are in my concrete case 10. In the end, I display the average time of the output operations (line 4).<\/p>\n<ul>\n<li><strong>Non-synchronized<\/strong><\/li>\n<\/ul>\n<p>The non-synchronized version of the program is quite similar to the synchronized version. Only the main function changed a bit.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ syncWithStdioPerformanceWithoutSync.cpp<\/span>\r\n\r\n... \r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(<span style=\"color: #007788; font-weight: bold;\">int<\/span> argc, <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> argv[]){\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>ios<span style=\"color: #555555;\">::<\/span>sync_with_stdio(<span style=\"color: #336666;\">false<\/span>);    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ get the filename<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>string myFile;\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( argc <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">2<\/span> ){\r\n        myFile<span style=\"color: #555555;\">=<\/span> argv[<span style=\"color: #ff6600;\">1<\/span>];\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n        std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Filename missing !\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n        exit(EXIT_FAILURE);\r\n    } \r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>ifstream file <span style=\"color: #555555;\">=<\/span> openFile(myFile);\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>string fileContent <span style=\"color: #555555;\">=<\/span> readFile(std<span style=\"color: #555555;\">::<\/span>move(file));\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> averageWithSync <span style=\"color: #555555;\">=<\/span> measureTime(iterations, [<span style=\"color: #555555;\">&amp;<\/span>fileContent]{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> writeToConsole(fileContent); });\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> averageWithoutSync <span style=\"color: #555555;\">=<\/span> measureTime(iterations, [<span style=\"color: #555555;\">&amp;<\/span>fileContent]{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> writeToConsole(fileContent); });\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Without Synchronisation: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> averageWithoutSync.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" seconds\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  \r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>I just added line (1) to the main program. Now, I hope for performance improvement.<\/p>\n<p>I did my performance test with a small program and a bigger text file (600.000 characters). The bigger file gave me no new insight; therefore, I skipped it.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555;\">&gt;&gt;<\/span> syncWithStdioPerformanceSync syncWithStdioPerformanceSync.cpp\r\n<span style=\"color: #555555;\">&gt;&gt;<\/span> syncWithStdioPerformanceWithoutSync syncWithStdioPerformanceSync.cpp\r\n<\/pre>\n<\/div>\n<h4>GCC<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5731\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppGcc.png\" alt=\"syncWithStdioPerformanceCppGcc\" width=\"500\" height=\"433\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppGcc.png 1056w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppGcc-300x260.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppGcc-1024x887.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppGcc-768x665.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h4>Clang<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5732\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang.png\" alt=\"syncWithStdioPerformanceCppClang\" width=\"500\" height=\"355\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang.png 2301w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang-300x213.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang-1024x726.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang-768x545.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang-1536x1089.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppClang-2048x1453.png 2048w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h4>cl.exe<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5733\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin.PNG\" alt=\"syncWithStdioPerformanceCppWin\" width=\"500\" height=\"353\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin.PNG 2302w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin-300x212.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin-1024x722.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin-768x542.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin-1536x1084.png 1536w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWin-2048x1445.png 2048w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>The results puzzled me because of Windows.<\/p>\n<ul>\n<li>With GCC, I had a performance improvement of about 70% in the non-synchronized variant.<\/li>\n<li>Neither Clang nor cl.exe showed any performance improvement. It seems that the non-synchronized in- and output operations are synchronized. My numbers proved my observation from the program <span style=\"font-family: courier new, courier;\">syncWithStdio.cpp<\/span>.<\/li>\n<li>Only for the record. Did you notice how slow the console on windows is?<\/li>\n<\/ul>\n<p>Of course, I&#8217;m guilty. I almost always break the following rule.<\/p>\n<h2><a href=\"https:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rio-endl\">SL.io.50: Avoid <code class=\"highlighter-rouge no-highlight\">endl<\/code><\/a><\/h2>\n<p>Why should you avoid <span style=\"font-family: courier new, courier;\">std::endl<\/span>? Or, to say it differently: What is the difference between the manipulator <span style=\"font-family: courier new, courier;\">std::endl<\/span> and &#8216;<span style=\"font-family: courier new, courier;\">\\n&#8217;<\/span>.<\/p>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">std::endl: <\/span>writes a new line and flushes the output buffer.<\/li>\n<li><span style=\"font-family: courier new, courier;\">&#8216;\\n&#8217;: <\/span>writes a new line.<span style=\"font-family: courier new, courier;\"><\/span><\/li>\n<\/ul>\n<p>Flushing the buffer is expensive and should, therefore, be avoided. If necessary, the buffer is automatically flushed. Honestly, I was curious to see the numbers. To make it extremely worse, here is my program, which puts a linebreak (line&nbsp; 3) after each character.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ syncWithStdioPerformanceEndl.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;fstream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;random&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;sstream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\nconstexpr <span style=\"color: #007788; font-weight: bold;\">int<\/span> iterations <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">500<\/span>;                                                    <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>ifstream openFile(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> myFile){                  \r\n\r\n  std<span style=\"color: #555555;\">::<\/span>ifstream file(myFile, std<span style=\"color: #555555;\">::<\/span>ios<span style=\"color: #555555;\">::<\/span>in);\r\n  <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( <span style=\"color: #555555;\">!<\/span>file ){\r\n    std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Can't open file \"<\/span><span style=\"color: #555555;\">+<\/span> myFile <span style=\"color: #555555;\">+<\/span> <span style=\"color: #cc3300;\">\"!\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    exit(EXIT_FAILURE);\r\n  }\r\n  <span style=\"color: #006699; font-weight: bold;\">return<\/span> file;\r\n  \r\n}\r\n\r\nstd<span style=\"color: #555555;\">::<\/span>string readFile(std<span style=\"color: #555555;\">::<\/span>ifstream file){                        \r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>stringstream buffer;\r\n    buffer <span style=\"color: #555555;\">&lt;&lt;<\/span> file.rdbuf();\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> buffer.str();\r\n    \r\n}\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> End<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> writeToConsole(<span style=\"color: #006699; font-weight: bold;\">const<\/span> std<span style=\"color: #555555;\">::<\/span>string<span style=\"color: #555555;\">&amp;<\/span> fileContent, End end){\r\n     \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> start <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now();\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #006699; font-weight: bold;\">auto<\/span> c<span style=\"color: #555555;\">:<\/span> fileContent) std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> c <span style=\"color: #555555;\">&lt;&lt;<\/span> end;                                 <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> dur <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>steady_clock<span style=\"color: #555555;\">::<\/span>now() <span style=\"color: #555555;\">-<\/span> start;\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> dur;\r\n}  \r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span> <span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Function<span style=\"color: #555555;\">&gt;<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> measureTime(std<span style=\"color: #555555;\">::<\/span><span style=\"color: #007788; font-weight: bold;\">size_t<\/span> iter, Function<span style=\"color: #555555;\">&amp;&amp;<\/span> f){\r\n    std<span style=\"color: #555555;\">::<\/span>chrono<span style=\"color: #555555;\">::<\/span>duration<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">double<\/span><span style=\"color: #555555;\">&gt;<\/span> dur{};\r\n    <span style=\"color: #006699; font-weight: bold;\">for<\/span> (<span style=\"color: #007788; font-weight: bold;\">int<\/span> i <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>; i <span style=\"color: #555555;\">&lt;<\/span> iter; <span style=\"color: #555555;\">++<\/span>i){\r\n        dur <span style=\"color: #555555;\">+=<\/span> f();\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> dur <span style=\"color: #555555;\">\/<\/span> iter;\r\n}\r\n    \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> main(<span style=\"color: #007788; font-weight: bold;\">int<\/span> argc, <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span> argv[]){\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n  \r\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ get the filename<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>string myFile;\r\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> ( argc <span style=\"color: #555555;\">==<\/span> <span style=\"color: #ff6600;\">2<\/span> ){\r\n        myFile<span style=\"color: #555555;\">=<\/span> argv[<span style=\"color: #ff6600;\">1<\/span>];\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">else<\/span>{\r\n        std<span style=\"color: #555555;\">::<\/span>cerr <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Filename missing !\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n        exit(EXIT_FAILURE);\r\n    } \r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>ifstream file <span style=\"color: #555555;\">=<\/span> openFile(myFile);\r\n  \r\n    std<span style=\"color: #555555;\">::<\/span>string fileContent <span style=\"color: #555555;\">=<\/span> readFile(std<span style=\"color: #555555;\">::<\/span>move(file));\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> averageWithFlush <span style=\"color: #555555;\">=<\/span> measureTime(iterations, \r\n                                        [<span style=\"color: #555555;\">&amp;<\/span>fileContent]{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> writeToConsole(fileContent, std<span style=\"color: #555555;\">::<\/span>endl<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char<\/span>, std<span style=\"color: #555555;\">::<\/span>char_traits<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">&gt;&gt;<\/span>); }); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">auto<\/span> averageWithoutFlush <span style=\"color: #555555;\">=<\/span> measureTime(iterations, [<span style=\"color: #555555;\">&amp;<\/span>fileContent]{ <span style=\"color: #006699; font-weight: bold;\">return<\/span> writeToConsole(fileContent, <span style=\"color: #cc3300;\">'\\n'<\/span>); });                     <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"With flush(std::endl) \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> averageWithFlush.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" seconds\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"Without flush(<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\\\<\/span><span style=\"color: #cc3300;\">n): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> averageWithoutFlush.count() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" seconds\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"With Flush\/Without Flush: \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> averageWithFlush<span style=\"color: #555555;\">\/<\/span>averageWithoutFlush <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In the first case, I did it with <span style=\"font-family: courier new, courier;\">std::e<\/span>ndl (line 2); in the second case, I did it with <span style=\"font-family: courier new, courier;\">&#8216;\\n<\/span>&#8216; (line 3). The program is quite similar to the previous one. The big difference is that I made 500 iterations (line 3). Why? I was astonished by the variations in the numbers. With a few iterations, I could not notice any difference. Sometimes, <span style=\"font-family: courier new, courier;\">std::endl<\/span> was two times faster than <span style=\"font-family: courier new, courier;\">&#8216;\\n&#8217;;<\/span> sometimes, <span style=\"font-family: courier new, courier;\">std::endl<\/span> was four times slower. I got similar behavior with cl.exe or with GCC.&nbsp; I also did it with another GCC or cl.exe compiler. Honestly, this was not what I expected. When I did it with 500 iterations, I got the expected winner. &#8216;<span style=\"font-family: courier new, courier;\">\\n<\/span>&#8216; seems to be 10% &#8211; 20% faster than<span style=\"font-family: courier new, courier;\"> std::endl.<\/span> <strong>Once more, only 10% &#8211; 20% faster.<\/strong><\/p>\n<h3>GCC<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5734\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppLinuxEndl.png\" alt=\"syncWithStdioPerformanceCppLinuxEndl\" width=\"500\" height=\"226\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppLinuxEndl.png 1072w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppLinuxEndl-300x136.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppLinuxEndl-1024x463.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppLinuxEndl-768x347.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h3>cl.exe<\/h3>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5735\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWinEndl.PNG\" alt=\"syncWithStdioPerformanceCppWinEndl\" width=\"500\" height=\"307\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWinEndl.PNG 1406w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWinEndl-300x184.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWinEndl-1024x629.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/07\/syncWithStdioPerformanceCppWinEndl-768x472.png 768w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h2>My Small Conclusion<\/h2>\n<p>I want to draw a small conclusion from my performance test.<\/p>\n<ul>\n<li><span style=\"font-family: courier new, courier;\">std::ios_base::sync_with_stdio(false)<\/span> can make a big difference on your platform, but you lose your thread-safety guarantee.<\/li>\n<li><span style=\"font-family: courier new, courier;\">std::endl i<\/span>s not as bad as its reputation. I will not change my habit.<\/li>\n<\/ul>\n<h2>What&#8217;s next?<\/h2>\n<p>Only one rule exists for the sections <span style=\"font-family: courier new, courier;\">regex,<\/span> <span style=\"font-family: courier new, courier;\">chrono,<\/span> and the C standard library. You see, I have to improvise in my <a href=\"https:\/\/www.modernescpp.com\/index.php\/regular-expressions\">next post<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As easy as my title and the rules of the C++ core guidelines sound, getting more performance out of the iostreams is no no-brainer.<\/p>\n","protected":false},"author":21,"featured_media":5725,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[447,470],"class_list":["post-5736","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-in-output","tag-performance"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5736"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5736\/revisions"}],"predecessor-version":[{"id":6781,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5736\/revisions\/6781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5725"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}