Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <cstring>
  2. #include <memory>
  3. #include <benchmark/benchmark.h>
  4.  
  5. void BenchCopy(benchmark::State& state)
  6. {
  7.   const auto data_size = static_cast<size_t>(state.range(0));
  8.   auto data1 = std::make_unique<char[]>(data_size);
  9.   auto data2 = std::make_unique<char[]>(data_size);
  10.  
  11.   // Set to some valid form of initialized data.
  12.   std::memset(data1.get(), 'x', data_size);
  13.  
  14.   while (state.KeepRunning())
  15.   {
  16.     std::copy(data1.get(), data1.get() + data_size, data2.get());
  17.   }
  18. }
  19.  
  20. void BenchMemcpy(benchmark::State& state)
  21. {
  22.   const auto data_size = static_cast<size_t>(state.range(0));
  23.   auto data1 = std::make_unique<char[]>(data_size);
  24.   auto data2 = std::make_unique<char[]>(data_size);
  25.  
  26.   // Set to some valid form of initialized data.
  27.   std::memset(data1.get(), 'x', data_size);
  28.  
  29.   while (state.KeepRunning())
  30.   {
  31.     std::memcpy(data2.get(), data1.get(), data_size);
  32.   }
  33. }
  34.  
  35. // Small copies
  36. BENCHMARK(BenchCopy)->Arg(100);
  37. BENCHMARK(BenchMemcpy)->Arg(100);
  38.  
  39. // Large copies
  40. BENCHMARK(BenchCopy)->Arg(209715200);
  41. BENCHMARK(BenchMemcpy)->Arg(209715200);
  42.  
  43. BENCHMARK_MAIN()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement