Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.44 KB | None | 0 0
  1. #include "BenchmarkIncludes.h"
  2. #include <map>
  3. #include <random>
  4. #include <Small.h>
  5. #include <Medium.h>
  6. #include <Large.h>
  7.  
  8. /*
  9. std::map
  10.  
  11.         /at, /operator[], /empty, /size, /max_size,
  12.         /clear, /insert, /erase, /swap,
  13.         count, find, equal_range, lower_bound, upper_bound
  14. */
  15.  
  16. static void pauseAndResume(State &state) {
  17.     for (auto _ :state) {
  18.         state.PauseTiming();
  19.         state.ResumeTiming();
  20.     }
  21. }
  22.  
  23. template<typename T>
  24. static void MapAtBench(State &state) {
  25.     auto N = state.range(0);
  26.     auto size = (std::size_t) N;
  27.     std::map<int,T> m;
  28.  
  29.     for(int i = 0; i < size; i++) {
  30.         auto container = T();
  31.         container.randomize();
  32.         m.insert(std::make_pair(i,container));
  33.     }
  34.  
  35.     int index = 0;
  36.  
  37.     for (auto _ : state) {
  38.         index++;
  39.         if (index >= size) {
  40.             index = 0;
  41.         }
  42.         DoNotOptimize(m.at(index));
  43.     }
  44.     state.SetComplexityN(state.range(0));
  45. }
  46.  
  47. template<typename T>
  48. static void MapSquareBracketsBench(State &state) {
  49.     auto N = state.range(0);
  50.     auto size = (std::size_t) N;
  51.     std::map<int,T> m;
  52.  
  53.     for(int i = 0; i < size; i++) {
  54.         auto container = T();
  55.         container.randomize();
  56.         m.insert(std::make_pair(i,container));
  57.     }
  58.     int index = 0;
  59.  
  60.     for (auto _ : state) {
  61.         index++;
  62.         if (index >= size) {
  63.             index = 0;
  64.         }
  65.         DoNotOptimize(m[index]);
  66.     }
  67.     state.SetComplexityN(state.range(0));
  68. }
  69.  
  70. template<typename T>
  71. static void MapEmptyBench(State &state) {
  72.     auto N = state.range(0);
  73.     auto size = (std::size_t) N;
  74.     std::map<int,T> m;
  75.  
  76.     int index = 0;
  77.  
  78.     for (auto _ : state) {
  79.         DoNotOptimize(m.empty());
  80.  
  81.         state.PauseTiming();
  82.             if(index > size) index = 0;
  83.             m.insert(std::make_pair(index++,T()));
  84.         state.ResumeTiming();
  85.     }
  86.     state.SetComplexityN(state.range(0));
  87. }
  88.  
  89. template<typename T>
  90. static void MapSizeBench(State &state) {
  91.     auto N = state.range(0);
  92.     auto size = (std::size_t) N;
  93.     std::map<int,T> m;
  94.  
  95.     int index = 0;
  96.  
  97.     for (auto _ : state) {
  98.         DoNotOptimize(m.size());
  99.  
  100.         state.PauseTiming();
  101.         if(index > size) index = 0;
  102.         m.insert(std::make_pair(index++,T()));
  103.         state.ResumeTiming();
  104.     }
  105.     state.SetComplexityN(state.range(0));
  106. }
  107.  
  108. template<typename T>
  109. static void MapMaxSizeBench(State &state) {
  110.     auto N = state.range(0);
  111.     auto size = (std::size_t) N;
  112.     std::map<int,T> m;
  113.  
  114.     int index = 0;
  115.  
  116.     for (auto _ : state) {
  117.         DoNotOptimize(m.max_size());
  118.  
  119.         state.PauseTiming();
  120.         if(index > size) index = 0;
  121.         m.insert(std::make_pair(index++,T()));
  122.         state.ResumeTiming();
  123.     }
  124.     state.SetComplexityN(state.range(0));
  125. }
  126.  
  127. template<typename T>
  128. static void MapClearBench(State &state) {
  129.     auto N = state.range(0);
  130.     auto size = (std::size_t) N;
  131.     std::map<int,T> m;
  132.  
  133.     for(int i = 0; i < size/2; i++) {
  134.         auto container = T();
  135.         container.randomize();
  136.         m.insert(std::make_pair(i,container));
  137.     }
  138.  
  139.     for (auto _ : state) {
  140.         m.clear();
  141.  
  142.         state.PauseTiming();
  143.         for(int i = 0; i < size/2; i++) {
  144.             auto container = T();
  145.             container.randomize();
  146.             m.insert(std::make_pair(i,container));
  147.         }
  148.         state.ResumeTiming();
  149.     }
  150.     state.SetComplexityN(state.range(0));
  151. }
  152.  
  153. template<typename T>
  154. static void MapInsertBench(State &state) {
  155.     auto N = state.range(0);
  156.     auto size = (std::size_t) N;
  157.     std::map<int,T> m;
  158.  
  159.     for(int i = 0; i < size; i++) {
  160.         auto container = T();
  161.         container.randomize();
  162.         m.insert(std::make_pair(i,container));
  163.     }
  164.     int index = 0;
  165.     for (auto _ : state) {
  166.         m.insert(std::make_pair(index++,T()));
  167.     }
  168.     state.SetComplexityN(state.range(0));
  169. }
  170.  
  171. template<typename T>
  172. static void MapEraseBench(State &state) {
  173.     auto N = state.range(0);
  174.     auto size = (std::size_t) N;
  175.     std::map<int,T> m;
  176.  
  177.     for(int i = 0; i < size; i++) {
  178.         auto container = T();
  179.         container.randomize();
  180.         m.insert(std::make_pair(i,container));
  181.     }
  182.     int index = 0;
  183.     for (auto _ : state) {
  184.         m.erase(index++);
  185.         state.PauseTiming();
  186.             if(index >= size) index = 0;
  187.             for(int i = 0; i < size; i++) {
  188.                 auto container = T();
  189.                 container.randomize();
  190.                 m.insert(std::make_pair(i,container));
  191.             }
  192.         state.ResumeTiming();
  193.     }
  194.  
  195.     state.SetComplexityN(state.range(0));
  196. }
  197.  
  198. template<typename T>
  199. static void MapSwapBench(State &state) {
  200.     auto N = state.range(0);
  201.     auto size = (std::size_t) N;
  202.     std::map<int,T> m1;
  203.     std::map<int,T> m2;
  204.  
  205.     for(int i = 0; i < size; i++) {
  206.         auto container = T();
  207.         container.randomize();
  208.         m1.insert(std::make_pair(i,container));
  209.     }
  210.  
  211.     for (auto _ : state) {
  212.         m2.swap(m1);
  213.  
  214.         state.PauseTiming();
  215.         m2.clear();
  216.         state.ResumeTiming();
  217.     }
  218.  
  219.     state.SetComplexityN(state.range(0));
  220. }
  221.  
  222. //BENCHMARK(pauseAndResume)->Range(1 << 4, 1 << 20);
  223. //
  224. //BENCHMARK_TEMPLATE(MapAtBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  225. //BENCHMARK_TEMPLATE(MapAtBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  226. //BENCHMARK_TEMPLATE(MapAtBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  227. //
  228. //BENCHMARK_TEMPLATE(MapSquareBracketsBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  229. //BENCHMARK_TEMPLATE(MapSquareBracketsBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  230. //BENCHMARK_TEMPLATE(MapSquareBracketsBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  231. //
  232. //BENCHMARK_TEMPLATE(MapEmptyBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  233. //BENCHMARK_TEMPLATE(MapEmptyBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  234. //BENCHMARK_TEMPLATE(MapEmptyBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  235. //
  236. //BENCHMARK_TEMPLATE(MapSizeBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  237. //BENCHMARK_TEMPLATE(MapSizeBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  238. //BENCHMARK_TEMPLATE(MapSizeBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  239. //
  240. //BENCHMARK_TEMPLATE(MapMaxSizeBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  241. //BENCHMARK_TEMPLATE(MapMaxSizeBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  242. //BENCHMARK_TEMPLATE(MapMaxSizeBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  243. //
  244. //BENCHMARK_TEMPLATE(MapClearBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  245. //BENCHMARK_TEMPLATE(MapClearBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  246. //BENCHMARK_TEMPLATE(MapClearBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  247. //
  248. //BENCHMARK_TEMPLATE(MapInsertBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  249. //BENCHMARK_TEMPLATE(MapInsertBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  250. //BENCHMARK_TEMPLATE(MapInsertBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  251. //
  252. //BENCHMARK_TEMPLATE(MapEraseBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  253. //BENCHMARK_TEMPLATE(MapEraseBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  254. //BENCHMARK_TEMPLATE(MapEraseBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
  255. //
  256. //BENCHMARK_TEMPLATE(MapSwapBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
  257. //BENCHMARK_TEMPLATE(MapSwapBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
  258. //BENCHMARK_TEMPLATE(MapSwapBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement