Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <algorithm>
  2. #include <vector>
  3.  
  4.  
  5. std::vector<int> v(1000);
  6. std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
  7.  
  8. static void StringCreation(benchmark::State& state) {
  9. // Code inside this loop is measured repeatedly
  10. for (auto _ : state) {
  11. std::string created_string("hello");
  12. // Make sure the variable is not optimized away by compiler
  13. benchmark::DoNotOptimize(created_string);
  14. std::for_each(begin(v), end(v), [](auto& i){ i += 2; });
  15. }
  16. }
  17. // Register the function as a benchmark
  18. BENCHMARK(StringCreation);
  19.  
  20. static void StringCopy(benchmark::State& state) {
  21. // Code before the loop is not measured
  22. std::string x = "hello";
  23. for (auto _ : state) {
  24. std::string copy(x);
  25. transform(begin(v), end(v), begin(v), [](const auto& i){ return i + 2; });
  26. }
  27. }
  28. BENCHMARK(StringCopy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement