https://godbolt.org/ http://quick-bench.com/oWbDZZ1lqXq6G69k0PnPtZu2V40 #include #include static void StringCreation(benchmark::State& state) { // Code inside this loop is measured repeatedly for (auto _ : state) { std::stringstream s; std::string created_string("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque accumsan, felis sed finibus tincidunt, mauris sapien tempus neque, sit amet egestas "); for (int i=0 ; i<20000 ; i++) { s << created_string; } // Make sure the variable is not optimized away by compiler benchmark::DoNotOptimize(s); } } // Register the function as a benchmark BENCHMARK(StringCreation); static void StringCopy(benchmark::State& state) { // Code before the loop is not measured for (auto _ : state) { std::string s = ""; std::string created_string("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque accumsan, felis sed finibus tincidunt, mauris sapien tempus neque, sit amet egestas "); for (int i=0 ; i<20000 ; i++) { s += created_string; } // Make sure the variable is not optimized away by compiler benchmark::DoNotOptimize(s); } } BENCHMARK(StringCopy); https://regexcrossword.com/