c++ - Benchmarking linefeed characters vs std::endl -



c++ - Benchmarking linefeed characters vs std::endl -

i'm reading “\n” or '\n' or std::endl std::cout?. despite consensus doesn't matter choose, decided build contrived test measure programme execution speed of each. here simple program:

#include <iostream> int main() { (std::size_t = 0; < iters; ++i) { #ifdef ver1 std::cout << "\n"; #endif #ifdef ver2 std::cout << '\n'; #endif #ifdef ver3 std::cout << std::endl; #endif } }

using 1 billion iterations , -o3 , redirecting output /dev/null/, these results:

class="lang-none prettyprint-override">"\n" 0:30.96 '\n' 0:31.66

with -o2:

class="lang-none prettyprint-override">"\n" 0:32.96 '\n' 0:31.54

why higher optimization level create '\n' slower?

with setup, you're measuring test run happened interrupted longest thread scheduler. also, imagine might different times writing real file instead of /dev/null.

c++

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -