Skip to content

Instantly share code, notes, and snippets.

@nthery
Last active September 30, 2020 17:43
Show Gist options
  • Save nthery/572c31a6b49485489f3d719265dea73c to your computer and use it in GitHub Desktop.
Save nthery/572c31a6b49485489f3d719265dea73c to your computer and use it in GitHub Desktop.
benchmark string copy vs move ctor
#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