I was going through a site to benchmark the small functions and methods in c++ for the purpose of optimization and performance and I found some results on strings that I am not able to figure out why. Link http://quick-bench.com/mNexo7ae75tYgt8HYoB17HEHgKc for the benchmark code.
static void StringCreation(benchmark::State& state) {
// Code inside this loop is measured repeatedly
for (auto _ : state) {
std::string created_string("hello");
// Make sure the variable is not optimized away by compiler
benchmark::DoNotOptimize(created_string);
}
}
// Register the function as a benchmark
BENCHMARK(StringCreation);
static void StringCreationMove(benchmark::State& state) {
// Code inside this loop is measured repeatedly
std::string x = "hello";
for (auto _ : state) {
std::string created_string_move(std::move(x));
// Make sure the variable is not optimized away by compiler
benchmark::DoNotOptimize(created_string_move);
}
}
// Register the function as a benchmark
BENCHMARK(StringCreationMove);
static void StringCopy(benchmark::State& state) {
// Code before the loop is not measured
std::string x = "hello";
for (auto _ : state) {
std::string copy(x.c_str());
benchmark::DoNotOptimize(copy);
}
}
BENCHMARK(StringCopy);
static void StringCopyByChar(benchmark::State& state) {
// Code before the loop is not measured
const char* x = "hello";
for (auto _ : state) {
std::string copyChar(x);
benchmark::DoNotOptimize(copyChar);
}
}
BENCHMARK(StringCopyByChar);