Skip to content

Instantly share code, notes, and snippets.

@BillyONeal
Created September 2, 2018 05:31
Show Gist options
  • Save BillyONeal/8691d0dc6a9437b44d71df303bad7a2a to your computer and use it in GitHub Desktop.
Save BillyONeal/8691d0dc6a9437b44d71df303bad7a2a to your computer and use it in GitHub Desktop.
#include <string>
#include <iterator>
#include <list>
#include <vector>
#include <random>
#include <benchmark/benchmark.h>
using std::string;
using std::vector;
using std::list;
using std::size;
using std::random_device;
using std::uniform_int_distribution;
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, 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);
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment