Last active
September 30, 2020 17:43
-
-
Save nthery/572c31a6b49485489f3d719265dea73c to your computer and use it in GitHub Desktop.
benchmark string copy vs move ctor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <benchmark/benchmark.h> | |
#include <string> | |
static void Copy(benchmark::State& state) { | |
// Code inside this loop is measured repeatedly | |
for (auto _ : state) { | |
std::string src("a very long string to defeat small string optimization"); | |
std::string dst{src}; | |
benchmark::DoNotOptimize(dst); | |
} | |
} | |
BENCHMARK(Copy); | |
static void Move(benchmark::State& state) { | |
for (auto _ : state) { | |
std::string src("a very long string to defeat small string optimization"); | |
std::string dst{std::move(src)}; | |
benchmark::DoNotOptimize(dst); | |
} | |
} | |
BENCHMARK(Move); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment