-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.cpp
154 lines (122 loc) · 4.41 KB
/
demo.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
//#pragma clang diagnostic ignored "-Wold-style-cast"
//#pragma clang diagnostic ignored "-Wnarrowing"
//#pragma execution_character_set("utf-8")
#include <chrono>
#include <iostream>
#include <sstream>
#include <iomanip>
#include "fssb/fixed_size_string_buffer.h"
#include "fssb/fixed_elem_size_queue.h"
#include "fssb/fixed_char_size_queue.h"
void demo();
void bench();
int main()
{
demo();
bench();
return 0;
}
void demo()
{
std::cout << "fixed_size_string_buffer demo \n\n";
constexpr size_t max_size = 10;
auto rb = fssb::FixedSizeStringBuffer<max_size>();
std::cout << " created ring buffer of " << max_size << " characters\n";
// add strings
const std::string str = "The Quick Brown Fox Jumped Over The Lazy Dog";
std::cout << " adding words to buffer from: '" << str << "'\n";
std::istringstream ss(str);
std::string word;
std::cout << rb;
while (ss >> word) {
rb.push(word);
std::cout << rb;
}
std::cout << " buffer free space is " << rb.free_space() << " characters\n";
std::cout << " pop() removing oldest surviving string: '" << rb.pop() << "'\n";
std::cout << " so now buffer looks like:\n\n";
std::cout << rb << "\n";
std::cout << " and buffer free space is " << rb.free_space() << " characters\n";
// iterate over all enteries in buffer
while (! rb.empty()) {
rb.pop();
}
std::cout << " result of pop() on all entries: \n\n";
std::cout << rb << "\n";
std::cout << " result of clear(): \n\n";
rb.clear();
std::cout << rb << "\n";
}
/*
* compare template has 3 params:
* LEN: length of test string used for benchmark
* CAPACITY: how many strings can the buffer hold
* EXCESS: 3 means additional buffer size if 0.3 * stringsize
*/
template <class T>
auto time_queue(T& q, std::string& str_test)
{
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
using std::chrono::steady_clock;
constexpr int num_iter = 1000 * 1000;
auto t1 = steady_clock::now();
for (auto i = 0; i < num_iter; ++i) {
q.push(str_test);
}
auto t2 = steady_clock::now();
auto delta = duration_cast<nanoseconds>(t2 - t1).count() / num_iter;
return delta;
}
template <size_t LEN, size_t CAPACITY, size_t EXCESS>
void compare()
{
using std::chrono::duration_cast;
using std::chrono::nanoseconds;
using std::chrono::steady_clock;
auto str_test = std::string(LEN, 'x');
// create buffer just enough size to hold str_capacity_in_buffer values
constexpr double str_capacity_in_buffer = CAPACITY + 0.1 * EXCESS;
constexpr auto max_size = static_cast<size_t>(LEN * str_capacity_in_buffer);
auto buf0 = fssb::FixedSizeStringBuffer<max_size>(); // opt0: char limit ring buffer
auto buf1 = fssb::FixedCharSizeQueue(max_size); // opt1: char limit std:queue
// time options
auto delta0 = time_queue(buf0, str_test);
auto delta1 = time_queue(buf1, str_test);
auto baseline = static_cast<long double>(delta0);
auto ratio1 = static_cast<long double>(delta1) / baseline;
using std::setw;
std::cout.precision(1);
// NOLINTBEGIN
std::cout << " │ " << setw(6) << LEN << " │ " << setw(8) << max_size << " │ "
<< setw(6) << delta0 << "ns │ " << setw(6) << delta1 << "ns │ "
<< std::fixed
<< setw(5) << ratio1 << "X │"
<< "\n";
// NOLINTEND
}
/// @brief simple benchmark
void bench()
{
std::cout << R"(
fixed_size_string_buffer :
wallclock time comparison for push operation
╭────────┬──────────┬──────────┬──────────┬────────╮
│ strlen │ max_size │ FixedSize│ FixedChar│ RATIO │
│ (chars)│ (chars) │ stringBuf│ std:queue│ │
│ │ │ (1) │ (2) │(2)/(1) │
├────────┼──────────┼──────────┼──────────┼────────┤
)";
// NOLINTBEGIN
compare<10,10,3>();
compare<100,10,3>();
compare<1000,10,3>();
// NOLINTEND
std::cout <<
R"( ╰────────┴──────────┴──────────┴──────────┴────────╯
(1) FixedSizeStringBuffer<max_size>()
(2) FixedCharSizeQueue(max_size)
max_size = (10.3 * strlen) characters
)";
}