-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_VS_list.cpp
More file actions
39 lines (33 loc) · 1.05 KB
/
vector_VS_list.cpp
File metadata and controls
39 lines (33 loc) · 1.05 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
#include <benchmark/benchmark.h>
#include <vector>
#include <list>
#include <numeric> // For std::iota to initialize vectors and lists
// Size of the container
const size_t N = 1 << 18; // 1 million elements
// Initialize a vector with N elements
static void BM_VectorIteration(benchmark::State& state) {
std::vector<int> vec(N);
std::iota(vec.begin(), vec.end(), 0); // Fill vector with 0, 1, 2, ...
for (auto _ : state) {
int sum = 0;
for (const auto& elem : vec) {
benchmark::DoNotOptimize(sum += elem);
}
benchmark::DoNotOptimize(sum);
}
}
BENCHMARK(BM_VectorIteration);
// Initialize a list with N elements
static void BM_ListIteration(benchmark::State& state) {
std::list<int> lst(N);
std::iota(lst.begin(), lst.end(), 0); // Fill list with 0, 1, 2, ...
for (auto _ : state) {
int sum = 0;
for (const auto& elem : lst) {
benchmark::DoNotOptimize(sum += elem);
}
benchmark::DoNotOptimize(sum);
}
}
BENCHMARK(BM_ListIteration);
BENCHMARK_MAIN();