Skip to content

Instantly share code, notes, and snippets.

@Ben1980

Ben1980/benchmark.cpp

Last active Nov 13, 2019
Embed
What would you like to do?
Container benchmark used on http://quick-bench.com
#include <vector>
#include <array>
#include <random>
#include <memory>
#include <algorithm>
const size_t COLUMNS = 500;
const size_t ROWS = 500;
static void TwoDimVector(benchmark::State& state) {
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_real_distribution<double> dist(0, 100);
std::vector<std::vector<double>> vec;
vec.reserve(ROWS);
for (int i=0; i < ROWS; ++i){
std::vector<double> row;
row.reserve(COLUMNS);
for (int j=0; j < COLUMNS; ++j){
row.push_back(dist(engine));
}
vec.push_back(row);
}
std::vector<std::vector<double>> res(ROWS, std::vector<double>(COLUMNS));
for (auto _ : state) {
for (size_t i = 0; i < ROWS; ++i) {
for (size_t k = 0; k < COLUMNS; ++k) {
for (size_t j = 0; j < COLUMNS; ++j) {
res[i][k] += vec[i][j] * vec[j][k];
}
}
}
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(TwoDimVector);
static void OneDimVector(benchmark::State& state) {
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_real_distribution<double> dist(0, 100);
std::vector<double> vec;
vec.reserve(ROWS);
for (int i=0; i < ROWS; ++i){
for (int j=0; j < COLUMNS; ++j){
vec.push_back(dist(engine));
}
}
std::vector<double> res(ROWS*COLUMNS);
for (auto _ : state) {
for (size_t i = 0; i < ROWS; ++i) {
for (size_t k = 0; k < COLUMNS; ++k) {
for (size_t j = 0; j < COLUMNS; ++j) {
res[i*COLUMNS + k] += vec[i*COLUMNS + j] * vec[j*COLUMNS + k];
}
}
}
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(OneDimVector);
static void OneDimStdArray(benchmark::State& state) {
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_real_distribution<double> dist(0, 100);
std::array<double, ROWS*COLUMNS> arr;
for (int i=0; i < ROWS*COLUMNS; ++i){
arr[i] = dist(engine);
}
std::array<double, ROWS*COLUMNS> res;
for (auto _ : state) {
for (size_t i = 0; i < ROWS; ++i) {
for (size_t k = 0; k < COLUMNS; ++k) {
for (size_t j = 0; j < COLUMNS; ++j) {
res[i*COLUMNS + k] += arr[i*COLUMNS + j] * arr[j*COLUMNS + k];
}
}
}
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(OneDimStdArray);
static void OneDimRawArray(benchmark::State& state) {
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_real_distribution<double> dist(0, 100);
double *arr = new double[ROWS*COLUMNS];
for (int i=0; i < ROWS*COLUMNS; ++i){
arr[i] = dist(engine);
}
double *res = new double[ROWS*COLUMNS];
std::fill(res, res + (ROWS*COLUMNS), 0);
for (auto _ : state) {
for (size_t i = 0; i < ROWS; ++i) {
for (size_t k = 0; k < COLUMNS; ++k) {
for (size_t j = 0; j < COLUMNS; ++j) {
res[i*COLUMNS + k] += arr[i*COLUMNS + j] * arr[j*COLUMNS + k];
}
}
}
benchmark::DoNotOptimize(res);
}
delete [] arr;
delete [] res;
}
BENCHMARK(OneDimRawArray);
static void OneDimRawArrayUniquePtr(benchmark::State& state) {
std::random_device seed;
std::mt19937 engine(seed());
std::uniform_real_distribution<double> dist(0, 100);
std::unique_ptr<double []> arr(new double[ROWS*COLUMNS]);
for (int i=0; i < ROWS*COLUMNS; ++i){
arr[i] = dist(engine);
}
std::unique_ptr<double []> res(new double[ROWS*COLUMNS]);
std::fill(res.get(), res.get() + (ROWS*COLUMNS), 0);
for (auto _ : state) {
for (size_t i = 0; i < ROWS; ++i) {
for (size_t k = 0; k < COLUMNS; ++k) {
for (size_t j = 0; j < COLUMNS; ++j) {
res[i*COLUMNS + k] += arr[i*COLUMNS + j] * arr[j*COLUMNS + k];
}
}
}
benchmark::DoNotOptimize(res);
}
}
BENCHMARK(OneDimRawArrayUniquePtr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.