Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "BenchmarkIncludes.h"
- #include <map>
- #include <random>
- #include <Small.h>
- #include <Medium.h>
- #include <Large.h>
- /*
- std::map
- /at, /operator[], /empty, /size, /max_size,
- /clear, /insert, /erase, /swap,
- count, find, equal_range, lower_bound, upper_bound
- */
- static void pauseAndResume(State &state) {
- for (auto _ :state) {
- state.PauseTiming();
- state.ResumeTiming();
- }
- }
- template<typename T>
- static void MapAtBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- for(int i = 0; i < size; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- int index = 0;
- for (auto _ : state) {
- index++;
- if (index >= size) {
- index = 0;
- }
- DoNotOptimize(m.at(index));
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapSquareBracketsBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- for(int i = 0; i < size; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- int index = 0;
- for (auto _ : state) {
- index++;
- if (index >= size) {
- index = 0;
- }
- DoNotOptimize(m[index]);
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapEmptyBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- int index = 0;
- for (auto _ : state) {
- DoNotOptimize(m.empty());
- state.PauseTiming();
- if(index > size) index = 0;
- m.insert(std::make_pair(index++,T()));
- state.ResumeTiming();
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapSizeBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- int index = 0;
- for (auto _ : state) {
- DoNotOptimize(m.size());
- state.PauseTiming();
- if(index > size) index = 0;
- m.insert(std::make_pair(index++,T()));
- state.ResumeTiming();
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapMaxSizeBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- int index = 0;
- for (auto _ : state) {
- DoNotOptimize(m.max_size());
- state.PauseTiming();
- if(index > size) index = 0;
- m.insert(std::make_pair(index++,T()));
- state.ResumeTiming();
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapClearBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- for(int i = 0; i < size/2; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- for (auto _ : state) {
- m.clear();
- state.PauseTiming();
- for(int i = 0; i < size/2; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- state.ResumeTiming();
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapInsertBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- for(int i = 0; i < size; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- int index = 0;
- for (auto _ : state) {
- m.insert(std::make_pair(index++,T()));
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapEraseBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m;
- for(int i = 0; i < size; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- int index = 0;
- for (auto _ : state) {
- m.erase(index++);
- state.PauseTiming();
- if(index >= size) index = 0;
- for(int i = 0; i < size; i++) {
- auto container = T();
- container.randomize();
- m.insert(std::make_pair(i,container));
- }
- state.ResumeTiming();
- }
- state.SetComplexityN(state.range(0));
- }
- template<typename T>
- static void MapSwapBench(State &state) {
- auto N = state.range(0);
- auto size = (std::size_t) N;
- std::map<int,T> m1;
- std::map<int,T> m2;
- for(int i = 0; i < size; i++) {
- auto container = T();
- container.randomize();
- m1.insert(std::make_pair(i,container));
- }
- for (auto _ : state) {
- m2.swap(m1);
- state.PauseTiming();
- m2.clear();
- state.ResumeTiming();
- }
- state.SetComplexityN(state.range(0));
- }
- //BENCHMARK(pauseAndResume)->Range(1 << 4, 1 << 20);
- //
- //BENCHMARK_TEMPLATE(MapAtBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapAtBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapAtBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapSquareBracketsBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapSquareBracketsBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapSquareBracketsBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapEmptyBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapEmptyBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapEmptyBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapSizeBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapSizeBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapSizeBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapMaxSizeBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapMaxSizeBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapMaxSizeBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapClearBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapClearBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapClearBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapInsertBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapInsertBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapInsertBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapEraseBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapEraseBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapEraseBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
- //
- //BENCHMARK_TEMPLATE(MapSwapBench, Small)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapSwapBench, Medium)->Range(1 << 4, 1 << 20)->Complexity();
- //BENCHMARK_TEMPLATE(MapSwapBench, Large)->Range(1 << 4, 1 << 10)->Complexity();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement