forked from stan-dev/perf-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
element_access.cpp
44 lines (36 loc) · 1001 Bytes
/
element_access.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <benchmark/benchmark.h>
#include <Eigen/Dense>
using Eigen::MatrixXd;
static void BM_EigenElementAccess(benchmark::State& state) {
auto m_d = MatrixXd::Random(500, 500).eval();
for (auto _ : state) {
benchmark::DoNotOptimize(m_d.data());
m_d(400);
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_EigenElementAccess);
static void BM_EigenCoeff(benchmark::State& state) {
auto m_d = MatrixXd::Random(500, 500).eval();
for (auto _ : state) {
// Actual task
benchmark::DoNotOptimize(m_d.data());
m_d.coeff(400);
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_EigenCoeff);
static void BM_VectorElementAccess(benchmark::State& state) {
auto m_d = MatrixXd::Random(500, 500).eval();
std::vector<double> v_d;
for (int i = 0; i < m_d.size(); i++) {
v_d.push_back(m_d(i));
}
for (auto _ : state) {
benchmark::DoNotOptimize(v_d.data());
v_d[400];
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_VectorElementAccess);
BENCHMARK_MAIN();