jaulib v1.4.1-17-gd77ace3-dirty
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
test_stringfmt_perf_int.cpp
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2024 Gothel Software e.K.
4 *
5 * ***
6 *
7 * SPDX-License-Identifier: MIT
8 *
9 * This Source Code Form is subject to the terms of the MIT License
10 * If a copy of the MIT was not distributed with this
11 * file, You can obtain one at https://opensource.org/license/mit/.
12 *
13 */
14#include <sys/types.h>
15#include <cassert>
16#include <cstdio>
17#include <cstring>
18#include <string_view>
19
20#include <jau/basic_types.hpp>
21#include <jau/cpp_lang_util.hpp>
22#include <jau/cpp_pragma.hpp>
23#include <jau/float_types.hpp>
24#include <jau/int_types.hpp>
25#include <jau/string_cfmt.hpp>
27#include <jau/string_util.hpp>
28#include <jau/test/catch2_ext.hpp>
29#include <jau/type_concepts.hpp>
31
32#ifdef HAS_STD_FORMAT
33 #include <format>
34#endif
35
36using namespace std::literals;
37
38using namespace jau::float_literals;
39
40using namespace jau::int_literals;
41
42TEST_CASE("jau_cfmt_benchmark_int0", "[benchmark][jau][std::string][format_int]") {
43 const size_t loops = 1000; // catch_auto_run ? 1000 : 1000;
44 WARN("Benchmark with " + std::to_string(loops) + " loops");
45 CHECK(true);
46
47 static constexpr const char *format_check_exp = "format_check: 3";
48 BENCHMARK("fmt1.32 format rsrved bench") {
49 int i1 = 3;
50
51 volatile size_t res = 0;
52 for( size_t i = 0; i < loops; ++i ) {
53 std::string s = jau::format_string("format_check: %d", i1);
54 REQUIRE(format_check_exp == s);
55 res = res + s.size();
56 }
57 return res;
58 };
59 BENCHMARK("fmtX.32 snprintf rsrved bench") {
60 int i1 = 3;
61
62 volatile size_t res = 0;
63 for( size_t i = 0; i < loops; ++i ) {
64 std::string s;
65 const size_t bsz = jau::cfmt::default_string_capacity + 1; // including EOS
66 s.reserve(bsz); // incl. EOS
67 s.resize(bsz - 1); // excl. EOS
68 size_t nchars = std::snprintf(&s[0], bsz, "format_check: %d", i1);
69 if( nchars < bsz ) {
70 s.resize(nchars);
71 }
72 REQUIRE(format_check_exp == s);
73 res = res + nchars;
74 }
75 return res;
76 };
77}
78
79TEST_CASE("jau_cfmt_benchmark_int1", "[benchmark][jau][std::string][format_int]") {
80 const size_t loops = 1000; // catch_auto_run ? 1000 : 1000;
81 WARN("Benchmark with " + std::to_string(loops) + " loops");
82 CHECK(true);
83
84 static constexpr const char *format_check_exp = "format_check: 003";
85 BENCHMARK("fmt1.32 format rsrved bench") {
86 int i1 = 3;
87
88 volatile size_t res = 0;
89 for( size_t i = 0; i < loops; ++i ) {
90 std::string s = jau::format_string("format_check: %03d", i1);
91 REQUIRE(format_check_exp == s);
92 res = res + s.size();
93 }
94 return res;
95 };
96 BENCHMARK("fmtX.32 snprintf rsrved bench") {
97 int i1 = 3;
98
99 volatile size_t res = 0;
100 for( size_t i = 0; i < loops; ++i ) {
101 std::string s;
102 const size_t bsz = jau::cfmt::default_string_capacity + 1; // including EOS
103 s.reserve(bsz); // incl. EOS
104 s.resize(bsz - 1); // excl. EOS
105 size_t nchars = std::snprintf(&s[0], bsz, "format_check: %03d", i1);
106 if( nchars < bsz ) {
107 s.resize(nchars);
108 }
109 REQUIRE(format_check_exp == s);
110 res = res + nchars;
111 }
112 return res;
113 };
114}
115
116/// Execute with `test_stringfmt_perf --perf-analysis`
117TEST_CASE("jau_cfmt_benchmark_int2", "[benchmark][jau][std::string][format_int]") {
118 const size_t loops = 1000; // catch_auto_run ? 1000 : 1000;
119 WARN("Benchmark with " + std::to_string(loops) + " loops");
120 CHECK(true);
121
122 static constexpr const std::string_view format_check_exp1 = "format_check: -1, 2";
123 int i1=-1;
124 size_t i2=2;
125
126 BENCHMARK("fmt1.130 formatR rsrved bench") {
127 volatile size_t res = 0;
128 for( size_t i = 0; i < loops; ++i ) {
129 std::string s;
131
132 jau::cfmt::formatR(s, "format_check: %d, %zu", i1, i2);
133 REQUIRE(format_check_exp1 == s);
134 res = res + s.size();
135 }
136 return res;
137 };
138 BENCHMARK("fmt1.132 format rsrved bench") {
139 volatile size_t res = 0;
140 for( size_t i = 0; i < loops; ++i ) {
141 std::string s = jau::format_string("format_check: %d, %zu", i1, i2);
142 REQUIRE(format_check_exp1 == s);
143 res = res + s.size();
144 }
145 return res;
146 };
147 BENCHMARK("fmtX.132 snprintf rsrved bench") {
148 volatile size_t res = 0;
149 for( size_t i = 0; i < loops; ++i ) {
150 std::string s;
151 const size_t bsz = jau::cfmt::default_string_capacity + 1; // including EOS
152 s.reserve(bsz); // incl. EOS
153 s.resize(bsz - 1); // excl. EOS
154 size_t nchars = std::snprintf(&s[0], bsz, "format_check: %d, %zu", i1, i2);
155 if( nchars < bsz ) {
156 s.resize(nchars);
157 }
158 REQUIRE(format_check_exp1 == s);
159 res = res + nchars;
160 }
161 return res;
162 };
163 BENCHMARK("fmt1.142 format bench") {
164 volatile size_t res = 0;
165 for( size_t i = 0; i < loops; ++i ) {
166 // fa += 0.01f; fb += 0.02f; ++sz1; ++i1; str1.append("X");
167 std::string s = jau::cfmt::format("format_check: %d, %zu", i1, i2);
168 REQUIRE(format_check_exp1 == s);
169 res = res + s.size();
170 }
171 return res;
172 };
173
174}
175
176TEST_CASE("jau_cfmt_benchmark_int_all", "[benchmark][jau][std::string][format_int]") {
177 const size_t loops = 1000; // catch_auto_run ? 1000 : 1000;
178 WARN("Benchmark with " + std::to_string(loops) + " loops");
179 CHECK(true);
180
181 static constexpr const std::string_view format_check_exp1 = "format_check: -1, 2, -3, 4, -5, 6, -7, 8, -9, 10";
182 static constexpr const std::string_view format_check_exp2 = "format_check: -1, 02, -03, 0004, -0005, 000006, -000007, 00000008, -00000009, 0000000010";
183 char i1=-1;
184 unsigned char i2=2;
185
186 short i3=-3;
187 unsigned short i4=4;
188
189 int i5=-5;
190 unsigned int i6=6;
191
192 long i7=-7;
193 unsigned long i8=8;
194
195 ssize_t i9 = -9;
196 size_t i10 = 10;
197
198 BENCHMARK("fmt1.130 formatR rsrved bench") {
199 volatile size_t res = 0;
200 for( size_t i = 0; i < loops; ++i ) {
201 std::string s;
203
204 jau::cfmt::formatR(s, "format_check: %hhd, %hhu, %hd, %hu, %d, %u, %ld, %lu, %zd, %zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
205 REQUIRE(format_check_exp1 == s);
206 res = res + s.size();
207 }
208 return res;
209 };
210 BENCHMARK("fmt1.132 format rsrved bench") {
211 volatile size_t res = 0;
212 for( size_t i = 0; i < loops; ++i ) {
213 std::string s = jau::format_string("format_check: %hhd, %hhu, %hd, %hu, %d, %u, %ld, %lu, %zd, %zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
214 REQUIRE(format_check_exp1 == s);
215 res = res + s.size();
216 }
217 return res;
218 };
219 BENCHMARK("fmtX.132 snprintf rsrved bench") {
220 volatile size_t res = 0;
221 for( size_t i = 0; i < loops; ++i ) {
222 std::string s;
223 const size_t bsz = jau::cfmt::default_string_capacity + 1; // including EOS
224 s.reserve(bsz); // incl. EOS
225 s.resize(bsz - 1); // excl. EOS
226 size_t nchars = std::snprintf(&s[0], bsz, "format_check: %hhd, %hhu, %hd, %hu, %d, %u, %ld, %lu, %zd, %zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
227 if( nchars < bsz ) {
228 s.resize(nchars);
229 }
230 REQUIRE(format_check_exp1 == s);
231 res = res + nchars;
232 }
233 return res;
234 };
235 BENCHMARK("fmt1.142 format bench") {
236 volatile size_t res = 0;
237 for( size_t i = 0; i < loops; ++i ) {
238 // fa += 0.01f; fb += 0.02f; ++sz1; ++i1; str1.append("X");
239 std::string s = jau::cfmt::format("format_check: %hhd, %hhu, %hd, %hu, %d, %u, %ld, %lu, %zd, %zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
240 REQUIRE(format_check_exp1 == s);
241 res = res + s.size();
242 }
243 return res;
244 };
245
246 BENCHMARK("fmtX.150 stringstream bench") {
247 volatile size_t res = 0;
248 for( size_t i = 0; i < loops; ++i ) {
249 std::ostringstream ss1;
250 ss1 << "format_check: "
251 << int(i1) << ", "
252 << unsigned(i2) << ", "
253 << i3 << ", "
254 << i4 << ", "
255 << i5 << ", "
256 << i6 << ", "
257 << i7 << ", "
258 << i8 << ", "
259 << i9 << ", "
260 << i10;
261 std::string s = ss1.str();
262 REQUIRE(format_check_exp1 == s);
263 res = res + s.size();
264 }
265 return res;
266 };
267
268 ///
269 ///
270
271 BENCHMARK("fmt1.230 formatR rsrved bench") {
272 volatile size_t res = 0;
273 for( size_t i = 0; i < loops; ++i ) {
274 std::string s;
276
277 jau::cfmt::formatR(s, "format_check: %01hhd, %02hhu, %03hd, %04hu, %05d, %06u, %07ld, %08lu, %09zd, %010zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
278 REQUIRE(format_check_exp2 == s);
279 res = res + s.size();
280 }
281 return res;
282 };
283 BENCHMARK("fmt1.232 format rsrved bench") {
284 volatile size_t res = 0;
285 for( size_t i = 0; i < loops; ++i ) {
286 std::string s = jau::format_string("format_check: %01hhd, %02hhu, %03hd, %04hu, %05d, %06u, %07ld, %08lu, %09zd, %010zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
287 REQUIRE(format_check_exp2 == s);
288 res = res + s.size();
289 }
290 return res;
291 };
292 BENCHMARK("fmtX.232 snprintf rsrved bench") {
293 volatile size_t res = 0;
294 for( size_t i = 0; i < loops; ++i ) {
295 std::string s;
296 const size_t bsz = jau::cfmt::default_string_capacity + 1; // including EOS
297 s.reserve(bsz); // incl. EOS
298 s.resize(bsz - 1); // excl. EOS
299 size_t nchars = std::snprintf(&s[0], bsz, "format_check: %01hhd, %02hhu, %03hd, %04hu, %05d, %06u, %07ld, %08lu, %09zd, %010zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
300 if( nchars < bsz ) {
301 s.resize(nchars);
302 }
303 REQUIRE(format_check_exp2 == s);
304 res = res + nchars;
305 }
306 return res;
307 };
308 BENCHMARK("fmt1.242 format bench") {
309 volatile size_t res = 0;
310 for( size_t i = 0; i < loops; ++i ) {
311 // fa += 0.01f; fb += 0.02f; ++sz1; ++i1; str1.append("X");
312 std::string s = jau::cfmt::format("format_check: %01hhd, %02hhu, %03hd, %04hu, %05d, %06u, %07ld, %08lu, %09zd, %010zu", i1, i2, i3, i4, i5, i6, i7, i8, i9, i10);
313 REQUIRE(format_check_exp2 == s);
314 res = res + s.size();
315 }
316 return res;
317 };
318
319}
Result formatR(std::string &s, size_t maxLen, std::string_view fmt, const Targs &...args) noexcept
Strict format with type validation of arguments against the format string, appending to the given des...
constexpr const size_t default_string_capacity
Default string reserved capacity w/o EOS (511)
std::string format(std::string_view fmt, const Targs &...args) noexcept
Strict format with type validation of arguments against the format string.
std::string format_string(std::string_view fmt, const Args &...args) noexcept
Safely returns a (non-truncated) string according to snprintf() formatting rules using a reserved str...
static int loops
TEST_CASE("jau_cfmt_benchmark_int0", "[benchmark][jau][std::string][format_int]")