2013-12-19 00:55:45 +00:00
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
2014-11-01 04:54:55 +00:00
|
|
|
#include <assert.h>
|
2013-12-19 00:55:45 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2015-10-08 09:43:45 +00:00
|
|
|
#include <cstdlib>
|
2014-01-07 21:14:41 +00:00
|
|
|
#include <iostream>
|
2013-12-19 00:55:45 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <list>
|
|
|
|
#include <map>
|
2014-08-04 10:38:37 +00:00
|
|
|
#include <mutex>
|
2013-12-19 00:55:45 +00:00
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
2014-08-08 09:22:31 +00:00
|
|
|
#include <string>
|
2013-12-19 00:55:45 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2015-03-06 17:35:00 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define BENCHMARK_NOINLINE __attribute__((noinline))
|
|
|
|
#else
|
|
|
|
# define BENCHMARK_NOINLINE
|
|
|
|
#endif
|
|
|
|
|
2013-12-19 00:55:45 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-03-06 17:35:00 +00:00
|
|
|
int BENCHMARK_NOINLINE Factorial(uint32_t n) {
|
2013-12-19 00:55:45 +00:00
|
|
|
return (n == 1) ? 1 : n * Factorial(n - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
double CalculatePi(int depth) {
|
|
|
|
double pi = 0.0;
|
|
|
|
for (int i = 0; i < depth; ++i) {
|
|
|
|
double numerator = static_cast<double>(((i % 2) * 2) - 1);
|
|
|
|
double denominator = static_cast<double>((2 * i) - 1);
|
|
|
|
pi += numerator / denominator;
|
|
|
|
}
|
|
|
|
return (pi - 1.0) * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::set<int> ConstructRandomSet(int size) {
|
|
|
|
std::set<int> s;
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
s.insert(i);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-08-04 10:38:37 +00:00
|
|
|
std::mutex test_vector_mu;
|
2014-01-07 21:14:41 +00:00
|
|
|
std::vector<int>* test_vector = nullptr;
|
2013-12-19 00:55:45 +00:00
|
|
|
|
|
|
|
} // end namespace
|
|
|
|
|
|
|
|
static void BM_Factorial(benchmark::State& state) {
|
|
|
|
int fac_42 = 0;
|
|
|
|
while (state.KeepRunning())
|
|
|
|
fac_42 = Factorial(8);
|
|
|
|
// Prevent compiler optimizations
|
2015-03-12 22:03:33 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << fac_42;
|
|
|
|
state.SetLabel(ss.str());
|
2013-12-19 00:55:45 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_Factorial);
|
2015-03-27 16:28:22 +00:00
|
|
|
BENCHMARK(BM_Factorial)->UseRealTime();
|
2015-03-11 16:47:15 +00:00
|
|
|
|
2013-12-19 00:55:45 +00:00
|
|
|
static void BM_CalculatePiRange(benchmark::State& state) {
|
|
|
|
double pi = 0.0;
|
|
|
|
while (state.KeepRunning())
|
|
|
|
pi = CalculatePi(state.range_x());
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << pi;
|
|
|
|
state.SetLabel(ss.str());
|
|
|
|
}
|
|
|
|
BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024);
|
2014-01-07 21:14:41 +00:00
|
|
|
|
2013-12-19 00:55:45 +00:00
|
|
|
static void BM_CalculatePi(benchmark::State& state) {
|
|
|
|
static const int depth = 1024;
|
|
|
|
while (state.KeepRunning()) {
|
2015-03-27 20:35:46 +00:00
|
|
|
benchmark::DoNotOptimize(CalculatePi(depth));
|
2013-12-19 00:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_CalculatePi)->Threads(8);
|
|
|
|
BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32);
|
|
|
|
BENCHMARK(BM_CalculatePi)->ThreadPerCpu();
|
2014-01-07 21:14:41 +00:00
|
|
|
|
2013-12-19 00:55:45 +00:00
|
|
|
static void BM_SetInsert(benchmark::State& state) {
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
state.PauseTiming();
|
|
|
|
std::set<int> data = ConstructRandomSet(state.range_x());
|
|
|
|
state.ResumeTiming();
|
|
|
|
for (int j = 0; j < state.range_y(); ++j)
|
|
|
|
data.insert(rand());
|
|
|
|
}
|
2014-01-07 22:31:14 +00:00
|
|
|
state.SetItemsProcessed(state.iterations() * state.range_y());
|
|
|
|
state.SetBytesProcessed(state.iterations() * state.range_y() * sizeof(int));
|
2013-12-19 00:55:45 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_SetInsert)->RangePair(1<<10,8<<10, 1,10);
|
|
|
|
|
2015-03-17 17:32:20 +00:00
|
|
|
template<typename Container, typename ValueType = typename Container::value_type>
|
2013-12-19 00:55:45 +00:00
|
|
|
static void BM_Sequential(benchmark::State& state) {
|
2015-03-17 17:32:20 +00:00
|
|
|
ValueType v = 42;
|
2014-04-17 17:04:02 +00:00
|
|
|
while (state.KeepRunning()) {
|
2015-03-17 17:32:20 +00:00
|
|
|
Container c;
|
2013-12-19 00:55:45 +00:00
|
|
|
for (int i = state.range_x(); --i; )
|
2015-03-17 17:32:20 +00:00
|
|
|
c.push_back(v);
|
2014-04-17 17:04:02 +00:00
|
|
|
}
|
2015-10-01 21:08:44 +00:00
|
|
|
const size_t items_processed = state.iterations() * state.range_x();
|
2013-12-19 00:55:45 +00:00
|
|
|
state.SetItemsProcessed(items_processed);
|
|
|
|
state.SetBytesProcessed(items_processed * sizeof(v));
|
|
|
|
}
|
2015-03-17 17:32:20 +00:00
|
|
|
BENCHMARK_TEMPLATE2(BM_Sequential, std::vector<int>, int)->Range(1 << 0, 1 << 10);
|
2013-12-19 00:55:45 +00:00
|
|
|
BENCHMARK_TEMPLATE(BM_Sequential, std::list<int>)->Range(1 << 0, 1 << 10);
|
2015-03-18 20:34:43 +00:00
|
|
|
// Test the variadic version of BENCHMARK_TEMPLATE in C++11 and beyond.
|
|
|
|
#if __cplusplus >= 201103L
|
|
|
|
BENCHMARK_TEMPLATE(BM_Sequential, std::vector<int>, int)->Arg(512);
|
|
|
|
#endif
|
2013-12-19 00:55:45 +00:00
|
|
|
|
|
|
|
static void BM_StringCompare(benchmark::State& state) {
|
|
|
|
std::string s1(state.range_x(), '-');
|
|
|
|
std::string s2(state.range_x(), '-');
|
|
|
|
while (state.KeepRunning())
|
2015-03-27 20:35:46 +00:00
|
|
|
benchmark::DoNotOptimize(s1.compare(s2));
|
2013-12-19 00:55:45 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_StringCompare)->Range(1, 1<<20);
|
|
|
|
|
|
|
|
static void BM_SetupTeardown(benchmark::State& state) {
|
2014-01-07 21:14:41 +00:00
|
|
|
if (state.thread_index == 0) {
|
|
|
|
// No need to lock test_vector_mu here as this is running single-threaded.
|
2013-12-19 00:55:45 +00:00
|
|
|
test_vector = new std::vector<int>();
|
2014-01-07 21:14:41 +00:00
|
|
|
}
|
|
|
|
int i = 0;
|
|
|
|
while (state.KeepRunning()) {
|
2014-08-04 10:38:37 +00:00
|
|
|
std::lock_guard<std::mutex> l(test_vector_mu);
|
2014-01-07 21:14:41 +00:00
|
|
|
if (i%2 == 0)
|
|
|
|
test_vector->push_back(i);
|
|
|
|
else
|
|
|
|
test_vector->pop_back();
|
|
|
|
++i;
|
|
|
|
}
|
2013-12-19 00:55:45 +00:00
|
|
|
if (state.thread_index == 0) {
|
|
|
|
delete test_vector;
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 21:14:41 +00:00
|
|
|
BENCHMARK(BM_SetupTeardown)->ThreadPerCpu();
|
2013-12-19 00:55:45 +00:00
|
|
|
|
|
|
|
static void BM_LongTest(benchmark::State& state) {
|
|
|
|
double tracker = 0.0;
|
2015-03-27 20:35:46 +00:00
|
|
|
while (state.KeepRunning()) {
|
2013-12-19 00:55:45 +00:00
|
|
|
for (int i = 0; i < state.range_x(); ++i)
|
2015-03-27 20:35:46 +00:00
|
|
|
benchmark::DoNotOptimize(tracker += i);
|
|
|
|
}
|
2013-12-19 00:55:45 +00:00
|
|
|
}
|
|
|
|
BENCHMARK(BM_LongTest)->Range(1<<16,1<<28);
|
|
|
|
|
2015-03-12 22:03:33 +00:00
|
|
|
BENCHMARK_MAIN()
|
2013-12-19 00:55:45 +00:00
|
|
|
|