-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
69 lines (58 loc) · 1.85 KB
/
vector.cpp
File metadata and controls
69 lines (58 loc) · 1.85 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <benchmark/benchmark.h>
#include <vector>
#include <chrono>
void BM_vector_add_ints(benchmark::State &state) {
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
while (state.KeepRunning()) {
std::vector<int> ivector;
for (auto i : arr) {
ivector.push_back(i);
}
}
}
BENCHMARK(BM_vector_add_ints);
void BM_vector_copy(benchmark::State &state) {
std::vector<int> ivector = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
while (state.KeepRunning()) {
auto other = ivector;
(void)other;
}
}
BENCHMARK(BM_vector_copy);
void BM_vector_copy_one(benchmark::State &state) {
std::vector<int> ivector = {0};
while (state.KeepRunning()) {
auto other = ivector;
(void)other;
}
}
BENCHMARK(BM_vector_copy_one);
using namespace std::chrono;
void BM_vector_insert_middle(benchmark::State &state) {
std::vector<int> ivector(2000, 0);
while (state.KeepRunning()) {
auto other = ivector;
auto begin = high_resolution_clock::now();
other.insert(std::next(other.begin(), 50), 0);
auto end = high_resolution_clock::now();
auto elapsed = duration_cast<duration<double>>(end - begin);
state.SetIterationTime(elapsed.count());
}
}
BENCHMARK(BM_vector_insert_middle)->UseManualTime();
struct Data {
int a = 0;
std::string b = "some";
};
void BM_vector_insert_middle_struct(benchmark::State &state) {
std::vector<Data> ivector(2000);
while (state.KeepRunning()) {
auto other = ivector;
auto begin = high_resolution_clock::now();
other.insert(std::next(other.begin(), 50), Data{});
auto end = high_resolution_clock::now();
auto elapsed = duration_cast<duration<double>>(end - begin);
state.SetIterationTime(elapsed.count());
}
}
BENCHMARK(BM_vector_insert_middle_struct)->UseManualTime();