add simple lookup bench

This commit is contained in:
2019-05-09 11:02:22 +07:00
parent 9f028a6ad4
commit 12ec532aa1
5 changed files with 208 additions and 0 deletions

8
scripts/bench_map_lookup.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/bash
set -e
BUILD_DIR=`dirname "$BASH_SOURCE"`/../build
cd $BUILD_DIR
./unbench/flat.hpp.unbench --benchmark_filter=_map_lookup --benchmark_format=csv > benchmark_map_lookup.csv
../scripts/bench_drawer.py -f benchmark_map_lookup.csv

8
scripts/bench_set_lookup.sh Executable file
View File

@@ -0,0 +1,8 @@
#!/bin/bash
set -e
BUILD_DIR=`dirname "$BASH_SOURCE"`/../build
cd $BUILD_DIR
./unbench/flat.hpp.unbench --benchmark_filter=_set_lookup --benchmark_format=csv > benchmark_set_lookup.csv
../scripts/bench_drawer.py -f benchmark_set_lookup.csv

View File

@@ -94,6 +94,18 @@ namespace flat_hpp_unbench
v = std::move(nv);
}
inline void generate_random_vector(std::size_t n, std::vector<std::pair<int,int>>& v) {
std::mt19937 engine(n);
std::uniform_int_distribution<int> dist;
std::vector<std::pair<int,int>> nv(n);
for ( std::size_t i = 0; i < n; ++i ) {
nv[i] = std::make_pair(dist(engine), dist(engine));
}
v = std::move(nv);
}
inline double min_bench_statistics(const std::vector<double>& v) {
return v.empty()
? 0.0

View File

@@ -0,0 +1,90 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/flat.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include "bench_base.hpp"
using namespace flat_hpp_unbench;
#include <flat_hpp/flat_map.hpp>
using namespace flat_hpp;
#ifdef BOOST_CONTAINER_FOUND
# include <boost/container/flat_map.hpp>
#endif
namespace
{
template < typename Value >
void flat_map_lookup(benchmark::State& state) {
std::vector<std::pair<int,int>> v;
generate_random_vector(state.range(), v);
flat_map<int, Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e.first));
benchmark::DoNotOptimize(s.find(e.second));
}
}
}
#ifdef BOOST_CONTAINER_FOUND
template < typename Value >
void boost_flat_map_lookup(benchmark::State& state) {
std::vector<std::pair<int,int>> v;
generate_random_vector(state.range(), v);
boost::container::flat_map<int, Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e.first));
benchmark::DoNotOptimize(s.find(e.second));
}
}
}
#endif
template < typename Value >
void std_map_lookup(benchmark::State& state) {
std::vector<std::pair<int,int>> v;
generate_random_vector(state.range(), v);
std::map<int, Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e.first));
benchmark::DoNotOptimize(s.find(e.second));
}
}
}
template < typename Value >
void std_unordered_map_lookup(benchmark::State& state) {
std::vector<std::pair<int,int>> v;
generate_random_vector(state.range(), v);
std::unordered_map<int, Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e.first));
benchmark::DoNotOptimize(s.find(e.second));
}
}
}
}
BENCHMARK_TEMPLATE(flat_map_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);
#ifdef BOOST_CONTAINER_FOUND
BENCHMARK_TEMPLATE(boost_flat_map_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);
#endif
BENCHMARK_TEMPLATE(std_map_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);
BENCHMARK_TEMPLATE(std_unordered_map_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);

View File

@@ -0,0 +1,90 @@
/*******************************************************************************
* This file is part of the "https://github.com/blackmatov/flat.hpp"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#include "bench_base.hpp"
using namespace flat_hpp_unbench;
#include <flat_hpp/flat_set.hpp>
using namespace flat_hpp;
#ifdef BOOST_CONTAINER_FOUND
# include <boost/container/flat_set.hpp>
#endif
namespace
{
template < typename Value >
void flat_set_lookup(benchmark::State& state) {
std::vector<int> v;
generate_random_vector(state.range(), v);
flat_set<Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e));
benchmark::DoNotOptimize(s.find(e * 2));
}
}
}
#ifdef BOOST_CONTAINER_FOUND
template < typename Value >
void boost_flat_set_lookup(benchmark::State& state) {
std::vector<int> v;
generate_random_vector(state.range(), v);
boost::container::flat_set<Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e));
benchmark::DoNotOptimize(s.find(e * 2));
}
}
}
#endif
template < typename Value >
void std_set_lookup(benchmark::State& state) {
std::vector<int> v;
generate_random_vector(state.range(), v);
std::set<Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e));
benchmark::DoNotOptimize(s.find(e * 2));
}
}
}
template < typename Value >
void std_unordered_set_lookup(benchmark::State& state) {
std::vector<int> v;
generate_random_vector(state.range(), v);
std::unordered_set<Value> s(v.begin(), v.end());
for ( auto _ : state ) {
for ( auto e : v ) {
benchmark::DoNotOptimize(s.find(e));
benchmark::DoNotOptimize(s.find(e * 2));
}
}
}
}
BENCHMARK_TEMPLATE(flat_set_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);
#ifdef BOOST_CONTAINER_FOUND
BENCHMARK_TEMPLATE(boost_flat_set_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);
#endif
BENCHMARK_TEMPLATE(std_set_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);
BENCHMARK_TEMPLATE(std_unordered_set_lookup, vec4)
->ComputeStatistics("min", min_bench_statistics)
->DenseRange(1,401,50);