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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
#include <charconv>
#include <cstring>
#include <random>
#include <vector>
#include "bencher/bencher.hpp"
#include "bencher/diagnostics.hpp"
#include "glaze/util/glaze_fast_float.hpp"
#include "glaze/util/simple_float.hpp"
// Generate test data
std::vector<float> generate_random_floats(size_t count, uint32_t seed = 12345)
{
std::mt19937 rng(seed);
std::uniform_int_distribution<uint32_t> dist;
std::vector<float> result;
result.reserve(count);
for (size_t i = 0; i < count; ++i) {
uint32_t bits = dist(rng);
float value;
std::memcpy(&value, &bits, sizeof(float));
// Skip NaN and Inf
if (!std::isnan(value) && !std::isinf(value)) {
result.push_back(value);
}
else {
--i; // retry
}
}
return result;
}
std::vector<double> generate_random_doubles(size_t count, uint32_t seed = 12345)
{
std::mt19937_64 rng(seed);
std::uniform_int_distribution<uint64_t> dist;
std::vector<double> result;
result.reserve(count);
for (size_t i = 0; i < count; ++i) {
uint64_t bits = dist(rng);
double value;
std::memcpy(&value, &bits, sizeof(double));
if (!std::isnan(value) && !std::isinf(value)) {
result.push_back(value);
}
else {
--i;
}
}
return result;
}
std::vector<std::string> serialize_floats(const std::vector<float>& floats)
{
std::vector<std::string> result;
result.reserve(floats.size());
char buf[32];
for (float f : floats) {
char* end = glz::simple_float::to_chars(buf, f);
result.emplace_back(buf, end - buf);
}
return result;
}
std::vector<std::string> serialize_doubles(const std::vector<double>& doubles)
{
std::vector<std::string> result;
result.reserve(doubles.size());
char buf[32];
for (double d : doubles) {
char* end = glz::simple_float::to_chars(buf, d);
result.emplace_back(buf, end - buf);
}
return result;
}
int main()
{
constexpr size_t N = 100000;
// Pre-generate test data
auto floats = generate_random_floats(N);
auto doubles = generate_random_doubles(N);
auto float_strings = serialize_floats(floats);
auto double_strings = serialize_doubles(doubles);
std::cout << "=== Float Serialization Benchmarks ===" << std::endl;
{
bencher::stage stage{"Float to_chars"};
stage.min_execution_count = 50;
stage.run("simple_float::to_chars (float)", [&] {
char buf[32];
size_t total_bytes = 0;
for (float f : floats) {
char* end = glz::simple_float::to_chars(buf, f);
total_bytes += (end - buf);
bencher::do_not_optimize(buf);
}
return total_bytes;
});
stage.run("std::to_chars (float)", [&] {
char buf[32];
size_t total_bytes = 0;
for (float f : floats) {
auto [ptr, ec] = std::to_chars(buf, buf + 32, f);
total_bytes += (ptr - buf);
bencher::do_not_optimize(buf);
}
return total_bytes;
});
bencher::print_results(stage);
}
std::cout << "\n=== Float Parsing Benchmarks ===" << std::endl;
{
bencher::stage stage{"Float from_chars"};
stage.min_execution_count = 50;
stage.run("simple_float::from_chars (float)", [&] {
float value;
size_t total_bytes = 0;
for (const auto& s : float_strings) {
glz::simple_float::from_chars<false>(s.data(), s.data() + s.size(), value);
total_bytes += s.size();
bencher::do_not_optimize(value);
}
return total_bytes;
});
stage.run("glz::from_chars/fast_float (float)", [&] {
float value;
size_t total_bytes = 0;
for (const auto& s : float_strings) {
glz::from_chars<false>(s.data(), s.data() + s.size(), value);
total_bytes += s.size();
bencher::do_not_optimize(value);
}
return total_bytes;
});
stage.run("std::from_chars (float)", [&] {
float value;
size_t total_bytes = 0;
for (const auto& s : float_strings) {
std::from_chars(s.data(), s.data() + s.size(), value);
total_bytes += s.size();
bencher::do_not_optimize(value);
}
return total_bytes;
});
bencher::print_results(stage);
}
std::cout << "\n=== Double Serialization Benchmarks ===" << std::endl;
{
bencher::stage stage{"Double to_chars"};
stage.min_execution_count = 50;
stage.run("simple_float::to_chars (double)", [&] {
char buf[32];
size_t total_bytes = 0;
for (double d : doubles) {
char* end = glz::simple_float::to_chars(buf, d);
total_bytes += (end - buf);
bencher::do_not_optimize(buf);
}
return total_bytes;
});
stage.run("std::to_chars (double)", [&] {
char buf[32];
size_t total_bytes = 0;
for (double d : doubles) {
auto [ptr, ec] = std::to_chars(buf, buf + 32, d);
total_bytes += (ptr - buf);
bencher::do_not_optimize(buf);
}
return total_bytes;
});
bencher::print_results(stage);
}
std::cout << "\n=== Double Parsing Benchmarks ===" << std::endl;
{
bencher::stage stage{"Double from_chars"};
stage.min_execution_count = 50;
stage.run("simple_float::from_chars (double)", [&] {
double value;
size_t total_bytes = 0;
for (const auto& s : double_strings) {
glz::simple_float::from_chars<false>(s.data(), s.data() + s.size(), value);
total_bytes += s.size();
bencher::do_not_optimize(value);
}
return total_bytes;
});
stage.run("glz::from_chars/fast_float (double)", [&] {
double value;
size_t total_bytes = 0;
for (const auto& s : double_strings) {
glz::from_chars<false>(s.data(), s.data() + s.size(), value);
total_bytes += s.size();
bencher::do_not_optimize(value);
}
return total_bytes;
});
stage.run("std::from_chars (double)", [&] {
double value;
size_t total_bytes = 0;
for (const auto& s : double_strings) {
std::from_chars(s.data(), s.data() + s.size(), value);
total_bytes += s.size();
bencher::do_not_optimize(value);
}
return total_bytes;
});
bencher::print_results(stage);
}
// Profile individual operations
std::cout << "\n=== Breakdown: simple_float parsing stages ===" << std::endl;
{
bencher::stage stage{"Parsing breakdown"};
stage.min_execution_count = 50;
// Just decimal parsing
stage.run("parse_decimal_strict only", [&] {
size_t total = 0;
for (const auto& s : float_strings) {
glz::simple_float::detail::decimal_number dec{};
const char* end =
glz::simple_float::detail::parse_decimal_strict<false>(s.data(), s.data() + s.size(), dec);
total += (end ? 1 : 0);
bencher::do_not_optimize(dec);
}
return total;
});
// Full parsing
stage.run("full from_chars", [&] {
size_t total = 0;
float value;
for (const auto& s : float_strings) {
auto [ptr, ec] = glz::simple_float::from_chars<false>(s.data(), s.data() + s.size(), value);
total += (ptr ? 1 : 0);
bencher::do_not_optimize(value);
}
return total;
});
bencher::print_results(stage);
}
return 0;
}
|