-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
98 lines (85 loc) · 2.05 KB
/
main.cpp
File metadata and controls
98 lines (85 loc) · 2.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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <memory>
#include <deque>
#include <random>
#include <benchmark/benchmark.h>
#include "ring_buffer.h"
struct Test
{
std::string s1;
int i1;
double d1;
Test(const std::string& s, int i, double d) : s1(s), i1(i), d1(d) {}
Test() {}
Test(const Test& test) noexcept : s1(test.s1),i1(test.i1),d1(test.d1) {
}
Test(const Test&& test) noexcept : s1(std::move(test.s1)),i1(std::move(test.i1)),d1(std::move(test.d1)) {
}
Test& operator=(const Test&& test) noexcept {
s1 = std::move(test.s1);
i1 = std::move(test.i1);
d1 = std::move(test.d1);
return *this;
}
Test& operator=(const Test& test) noexcept {
s1 = test.s1;
i1 = test.i1;
d1 = test.d1;
return *this;
}
};
static constexpr int BUF_SIZE = 1000;
static constexpr int ITERATIONS = 100000;
static constexpr int INIT_SIZE = 100;
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<int> dist(1, 2);
std::deque<Test> cbuf2(1000);
RingBuffer<Test> cbuf1(1000);
static void BM_OwnRingBuffer(benchmark::State &state)
{
Test test;
for (auto _ : state)
{
for (int i = 0; i < INIT_SIZE; i++)
{
cbuf1.emplace(Test());
}
for (int i = 0; i < ITERATIONS; i++)
{
if (dist(mt) == 1)
{
cbuf1.emplace(Test());
}
else
{
if (!cbuf1.isEmpty())
cbuf1.pop();
}
}
}
}
static void BM_StdDeque(benchmark::State &state)
{
for (auto _ : state)
{
for (int i = 0; i < INIT_SIZE; i++)
{
cbuf2.push_back(Test());
}
for (int i = 0; i < ITERATIONS; i++)
{
if (dist(mt) == 1)
{
cbuf2.push_back(Test());
}
else
{
if (!cbuf2.empty())
cbuf2.pop_front();
}
}
}
}
BENCHMARK(BM_OwnRingBuffer);
BENCHMARK(BM_StdDeque);
BENCHMARK_MAIN();