forked from stan-dev/perf-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdvectorfun.cpp
39 lines (34 loc) · 919 Bytes
/
stdvectorfun.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
#include <benchmark/benchmark.h>
const int kSize = 1000;
static void push(benchmark::State& state) {
for (auto _ : state) {
std::vector<double> v;
benchmark::DoNotOptimize(v.data());
for (int i = 0; i < kSize; ++i)
v.push_back(i + 27.2);
benchmark::ClobberMemory();
}
}
BENCHMARK(push);
static void reserve_push(benchmark::State& state) {
for (auto _ : state) {
std::vector<double> v;
v.reserve(kSize);
benchmark::DoNotOptimize(v.data());
for (int i = 0; i < kSize; ++i)
v.push_back(i + 27.2);
benchmark::ClobberMemory();
}
}
BENCHMARK(reserve_push);
static void initialize_op_assign(benchmark::State& state) {
for (auto _ : state) {
std::vector<double> v(kSize);
benchmark::DoNotOptimize(v.data());
for (int i = 0; i < kSize; ++i)
v[i] = i + 27.2;
benchmark::ClobberMemory();
}
}
BENCHMARK(initialize_op_assign);
BENCHMARK_MAIN();