{"id":19044,"date":"2025-08-20T11:02:52","date_gmt":"2025-08-20T11:02:52","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=19044"},"modified":"2025-08-22T08:35:15","modified_gmt":"2025-08-22T08:35:15","slug":"the-evolution-of-writing-output-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=19044","title":{"rendered":"The evolution of writing output (C++)"},"content":{"rendered":"<h2>Scenarios<\/h2>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;chrono&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;fstream&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;print&gt;\r\n#include &lt;string&gt;\r\n#include &lt;string_view&gt;\r\n\r\nint main()\r\n{\r\n    using namespace std::chrono;\r\n\r\n    \/\/ -------------- C style (std::printf \/ std::fprintf) ----------\r\n    {\r\n        auto start = high_resolution_clock::now();\r\n\r\n        const char* msg = &quot;message content&quot;;\r\n        std::FILE* fp = fopen(&quot;file_c.txt&quot;, &quot;w&quot;);\r\n        for (uint32_t i = 0; i &lt; 100000; ++i)\r\n        {\r\n            auto now = system_clock::now();\r\n            std::time_t time = system_clock::to_time_t(now);\r\n            std::tm local = *std::localtime(&amp;time);\r\n\r\n            std::fprintf(fp, &quot;%04d-%02d-%02dT%02d:%02d:%02d %s\\n&quot;,\r\n                local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,\r\n                local.tm_hour, local.tm_min, local.tm_sec, msg);\r\n            \/\/ std::printf for stdout\r\n        }\r\n        std::fflush(fp);\r\n        std::fclose(fp);\r\n\r\n        auto end = high_resolution_clock::now();\r\n        std::println(&quot;{:&lt;20}{:&gt;10}ns&quot;,\r\n            std::string{&quot;C-style took:&quot;},\r\n            duration_cast&lt;nanoseconds&gt;(end - start).count());\r\n    }\r\n\r\n    \/\/ -------------- C++ style (std::cout \/ std::ofstream) ---------\r\n    {\r\n        auto start = high_resolution_clock::now();\r\n\r\n        std::string msg = &quot;message content&quot;;\r\n        std::ofstream ofs(&quot;file_cpp_old.txt&quot;);\r\n        for (uint32_t i = 0; i &lt; 100000; ++i)\r\n        {\r\n            auto now = system_clock::now();\r\n            std::time_t time = system_clock::to_time_t(now);\r\n            std::tm local = *std::localtime(&amp;time);\r\n\r\n            ofs &lt;&lt; std::setfill(&#039;0&#039;)\r\n                &lt;&lt; std::setw(4) &lt;&lt; (local.tm_year + 1900) &lt;&lt; &quot;-&quot;\r\n                &lt;&lt; std::setw(2) &lt;&lt; (local.tm_mon + 1) &lt;&lt; &quot;-&quot;\r\n                &lt;&lt; std::setw(2) &lt;&lt; local.tm_mday &lt;&lt; &quot;T&quot;\r\n                &lt;&lt; std::setw(2) &lt;&lt; local.tm_hour &lt;&lt; &quot;:&quot;\r\n                &lt;&lt; std::setw(2) &lt;&lt; local.tm_min &lt;&lt; &quot;:&quot;\r\n                &lt;&lt; std::setw(2) &lt;&lt; local.tm_sec &lt;&lt; &quot; &quot;\r\n                &lt;&lt; msg &lt;&lt; &quot;\\n&quot;;\r\n            \/\/ std::cout for stdout\r\n        }\r\n        ofs.flush();\r\n        ofs.close();\r\n\r\n        auto end = high_resolution_clock::now();\r\n        std::println(&quot;{:&lt;20}{:&gt;10}ns&quot;,\r\n            std::string{&quot;Old C++-style took:&quot;},\r\n            duration_cast&lt;nanoseconds&gt;(end - start).count());\r\n    }\r\n\r\n    \/\/ -------------- C++ style (std::print \/ std::print) -----------\r\n    {\r\n        auto start = high_resolution_clock::now();\r\n\r\n        std::string_view msg = &quot;message content&quot;;\r\n        std::FILE* fp = std::fopen(&quot;file_c++_new.txt&quot;, &quot;w&quot;);\r\n        for (uint32_t i = 0; i &lt; 100000; ++i)\r\n        {\r\n            auto now = system_clock::now();\r\n            std::time_t time = system_clock::to_time_t(now);\r\n            std::tm local = *std::localtime(&amp;time);\r\n\r\n            std::print(fp, &quot;{:04}-{:02}-{:02}T{:02}:{:02}:{:02} {}\\n&quot;,\r\n                local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,\r\n                local.tm_hour, local.tm_min, local.tm_sec, msg);\r\n            \/\/ std::print for stdout\r\n        }\r\n        std::fflush(fp);\r\n        std::fclose(fp);\r\n\r\n        auto end = high_resolution_clock::now();\r\n        std::println(&quot;{:&lt;20}{:&gt;10}ns&quot;,\r\n            std::string{&quot;New C++-style took:&quot;},\r\n            duration_cast&lt;nanoseconds&gt;(end - start).count());\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<pre>\r\n% uname -a\r\nDarwin FMXK77H3WK 24.6.0 Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:55 PDT 2025; root:xnu-11417.140.69~1\/RELEASE_ARM64_T6031 arm64\r\n% g++ --version\r\nApple clang version 17.0.0 (clang-1700.0.13.5)\r\nTarget: arm64-apple-darwin24.6.0\r\n% g++ main.cpp -O3 --std=c++23\r\n% .\/a.out                     \r\nC-style took:         48350334ns\r\nOld C++-style took:   54821291ns\r\nNew C++-style took:   84056042ns\r\n<\/pre>\n<hr>\n<h2>Comparison<\/h2>\n<table>\n<tr>\n<th>Aspect<\/th>\n<th>C-style<\/th>\n<th>C++-style (< C++23)<\/th>\n<th>C++-style (>= C++23)<\/th>\n<\/tr>\n<tr>\n<th>Type safety<\/th>\n<td>Not type-safe<\/td>\n<td>Type-safe<\/td>\n<td>Strong type-safety<\/td>\n<\/tr>\n<tr>\n<th>Verbosity<\/th>\n<td>Low<\/td>\n<td>High (lots of &#8220;<code>>><\/code>&#8220;)<\/td>\n<td>Very low<\/td>\n<\/tr>\n<tr>\n<th>Performance *<\/th>\n<td>Best<\/td>\n<td>Good<\/td>\n<td>Worst<\/td>\n<\/tr>\n<\/table>\n<p>* Depends a lot on the use case. I will stick with the result of the test. Lots of writes into a file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scenarios % uname -a Darwin FMXK77H3WK 24.6.0 Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:55 PDT 2025; root:xnu-11417.140.69~1\/RELEASE_ARM64_T6031 arm64 %<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19044"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19044"}],"version-history":[{"count":38,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19044\/revisions"}],"predecessor-version":[{"id":19082,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19044\/revisions\/19082"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19044"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}