Skip to content

Instantly share code, notes, and snippets.

@BillyONeal
Created September 5, 2018 21:15
Show Gist options
  • Save BillyONeal/1cd48777c94ab0a13d863296508ca1ba to your computer and use it in GitHub Desktop.
Save BillyONeal/1cd48777c94ab0a13d863296508ca1ba to your computer and use it in GitHub Desktop.
#include <string>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <vector>
#include <random>
#include <benchmark/benchmark.h>
using namespace std;
const int stringSize = 20;
const int stringCount = 10000;
const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
struct DataFixture {
vector<string> data;
DataFixture() {
random_device rd;
uniform_int_distribution<int> dist(0, static_cast<int>(size(letters) - 1));
data.reserve(stringCount);
for (int str = 0; str < stringCount; ++str) {
for (char& ch : data.emplace_back(stringSize, '\0')) {
ch = letters[dist(rd)];
}
}
}
};
DataFixture fixture;
void ListAssign(benchmark::State& state) {
list<string> unitUnderTest;
for (auto&& _ : state) {
(void)_;
unitUnderTest.assign(fixture.data.begin(), fixture.data.end());
}
}
BENCHMARK(ListAssign);
template<template <class...> class MapType>
struct MapDataFixture {
MapType<int, string> data;
MapDataFixture() {
int num = 0;
for (auto&& entry : fixture.data) {
data.emplace(num++, entry);
}
}
};
template<template <class...> class MapType>
MapDataFixture<MapType> mapFixture;
template<template <class...> class MapType>
void MapCopyAssign(benchmark::State& state) {
MapType<int, string> unitUnderTest;
for (auto&& _ : state) {
(void)_;
unitUnderTest = mapFixture<MapType>.data;
}
}
BENCHMARK_TEMPLATE1(MapCopyAssign, map);
BENCHMARK_TEMPLATE1(MapCopyAssign, unordered_map);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment