-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmemory_bench.hpp
More file actions
197 lines (164 loc) · 5.24 KB
/
memory_bench.hpp
File metadata and controls
197 lines (164 loc) · 5.24 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once
#include <benchmark/benchmark.h>
#include <string>
#include <vector>
// Run-length encoding: compress consecutive repeated characters
// Example: "aaabbbccc" -> "3a3b3c"
// NOTE: Intentionally inefficient - no pre-allocation to show multiple
// allocations
static std::string rle_encode(const std::string& input) {
if (input.empty()) return "";
std::string result; // No reserve - will trigger multiple reallocations
char current = input[0];
size_t count = 1;
for (size_t i = 1; i < input.size(); ++i) {
if (input[i] == current) {
count++;
} else {
// Create intermediate strings for each run
std::string count_str = std::to_string(count);
std::string run_encoded = count_str + current;
result += run_encoded; // Concatenation causes reallocations
current = input[i];
count = 1;
}
}
// Final run
std::string count_str = std::to_string(count);
std::string final_run = count_str + current;
result += final_run;
return result;
}
// Run-length decoding: decompress RLE encoded string
// Example: "3a3b3c" -> "aaabbbccc"
static std::string rle_decode(const std::string& input) {
std::string result;
size_t i = 0;
while (i < input.size()) {
// Parse the count
size_t count = 0;
while (i < input.size() && std::isdigit(input[i])) {
count = count * 10 + (input[i] - '0');
i++;
}
// Get the character
if (i < input.size()) {
char ch = input[i];
result.append(count, ch);
i++;
}
}
return result;
}
// Generate a string with patterns for RLE
static std::string generate_rle_input(size_t size, size_t run_length) {
std::string result;
result.reserve(size);
const std::string chars = "abcdefghijklmnopqrstuvwxyz";
size_t char_idx = 0;
while (result.size() < size) {
size_t count = std::min(run_length, size - result.size());
result.append(count, chars[char_idx % chars.size()]);
char_idx++;
}
return result;
}
// Benchmark: RLE encoding with small runs (high compression)
static void BM_RLE_Encode_SmallRuns(benchmark::State& state) {
const size_t input_size = state.range(0);
std::string input = generate_rle_input(input_size, 3);
for (auto _ : state) {
std::string encoded = rle_encode(input);
benchmark::DoNotOptimize(encoded);
benchmark::ClobberMemory();
}
state.SetBytesProcessed(state.iterations() * input_size);
}
BENCHMARK(BM_RLE_Encode_SmallRuns)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);
// Benchmark: RLE encoding with large runs (low compression)
static void BM_RLE_Encode_LargeRuns(benchmark::State& state) {
const size_t input_size = state.range(0);
std::string input = generate_rle_input(input_size, 100);
for (auto _ : state) {
std::string encoded = rle_encode(input);
benchmark::DoNotOptimize(encoded);
benchmark::ClobberMemory();
}
state.SetBytesProcessed(state.iterations() * input_size);
}
BENCHMARK(BM_RLE_Encode_LargeRuns)
->Arg(100)
->Arg(1000)
->Arg(10000)
->Arg(100000);
// Benchmark: RLE decoding
static void BM_RLE_Decode(benchmark::State& state) {
const size_t input_size = state.range(0);
std::string input = generate_rle_input(input_size, 10);
std::string encoded = rle_encode(input);
for (auto _ : state) {
std::string decoded = rle_decode(encoded);
benchmark::DoNotOptimize(decoded);
benchmark::ClobberMemory();
}
state.SetBytesProcessed(state.iterations() * encoded.size());
}
BENCHMARK(BM_RLE_Decode)->Arg(100)->Arg(1000)->Arg(10000)->Arg(100000);
// Benchmark: Vector allocations (resizing pattern)
static void BM_Vector_PushBack(benchmark::State& state) {
const size_t count = state.range(0);
for (auto _ : state) {
std::vector<int> vec;
for (size_t i = 0; i < count; ++i) {
vec.push_back(static_cast<int>(i));
}
benchmark::DoNotOptimize(vec);
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_Vector_PushBack)->Arg(10)->Arg(100)->Arg(1000)->Arg(10000);
// Benchmark: Vector allocations with reserve (optimized)
static void BM_Vector_Reserve(benchmark::State& state) {
const size_t count = state.range(0);
for (auto _ : state) {
std::vector<int> vec;
vec.reserve(count);
for (size_t i = 0; i < count; ++i) {
vec.push_back(static_cast<int>(i));
}
benchmark::DoNotOptimize(vec);
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_Vector_Reserve)->Arg(10)->Arg(100)->Arg(1000)->Arg(10000);
// Benchmark: String concatenation (many allocations)
static void BM_String_Concatenation(benchmark::State& state) {
const size_t count = state.range(0);
for (auto _ : state) {
std::string result;
for (size_t i = 0; i < count; ++i) {
result += "x";
}
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_String_Concatenation)->Arg(10)->Arg(100)->Arg(1000)->Arg(10000);
// Benchmark: String concatenation with reserve (optimized)
static void BM_String_Reserve(benchmark::State& state) {
const size_t count = state.range(0);
for (auto _ : state) {
std::string result;
result.reserve(count);
for (size_t i = 0; i < count; ++i) {
result += "x";
}
benchmark::DoNotOptimize(result);
benchmark::ClobberMemory();
}
}
BENCHMARK(BM_String_Reserve)->Arg(10)->Arg(100)->Arg(1000)->Arg(10000);