Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <vector>
- std::vector<int> v(1000);
- std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
- 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);
- std::for_each(begin(v), end(v), [](auto& i){ i += 2; });
- }
- }
- // Register the function as a benchmark
- BENCHMARK(StringCreation);
- static void StringCopy(benchmark::State& state) {
- // Code before the loop is not measured
- std::string x = "hello";
- for (auto _ : state) {
- std::string copy(x);
- transform(begin(v), end(v), begin(v), [](const auto& i){ return i + 2; });
- }
- }
- BENCHMARK(StringCopy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement