124TEST_CASE(
"jau_cfmt_benchmark_all",
"[benchmark][jau][std::string][format_string]") {
125 const size_t loops = 1000;
126 WARN(
"Benchmark with " + std::to_string(
loops) +
" loops");
129 static constexpr const char *format_check_exp =
"format_check: 1.10, 2.20, 1, 2, 003, Hi World";
131 BENCHMARK(
"fmt1.01 check bench") {
132 float fa = 1.1f, fb = 2.2f;
136 std::string str1 =
"Hi World";
138 volatile size_t res = 0;
139 for(
size_t i = 0; i <
loops; ++i ) {
140 ssize_t r =
jau::cfmt::check(
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1);
146 BENCHMARK(
"fmt1.02 checkR bench") {
147 float fa = 1.1f, fb = 2.2f;
151 std::string str1 =
"Hi World";
153 volatile size_t res = 0;
154 for(
size_t i = 0; i <
loops; ++i ) {
155 jau::cfmt::Result pc =
jau::cfmt::checkR(
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1);
161 BENCHMARK(
"fmt1.20 format-ckd rsrved bench") {
162 float fa = 1.1f, fb = 2.2f;
166 std::string str1 =
"Hi World";
168 volatile size_t res = 0;
169 for(
size_t i = 0; i <
loops; ++i ) {
170 std::string s =
jau_format_string(
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1);
171 REQUIRE(format_check_exp == s);
172 res = res + s.size();
176 BENCHMARK(
"fmt1.30 formatR rsrved bench") {
177 float fa = 1.1f, fb = 2.2f;
181 std::string str1 =
"Hi World";
183 volatile size_t res = 0;
184 for(
size_t i = 0; i <
loops; ++i ) {
188 jau::cfmt::formatR(s,
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1);
189 REQUIRE(format_check_exp == s);
190 res = res + s.size();
194 BENCHMARK(
"fmt1.32 format rsrved bench") {
195 float fa = 1.1f, fb = 2.2f;
199 std::string str1 =
"Hi World";
201 volatile size_t res = 0;
202 for(
size_t i = 0; i <
loops; ++i ) {
203 std::string s =
jau::format_string(
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1);
204 REQUIRE(format_check_exp == s);
205 res = res + s.size();
209 BENCHMARK(
"fmtX.32 snprintf rsrved bench") {
210 float fa = 1.1f, fb = 2.2f;
214 std::string str1 =
"Hi World";
216 volatile size_t res = 0;
217 for(
size_t i = 0; i <
loops; ++i ) {
222 size_t nchars = std::snprintf(&s[0], bsz,
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1.c_str());
226 REQUIRE(format_check_exp == s);
231 BENCHMARK(
"fmt1.42 format bench") {
232 float fa = 1.1f, fb = 2.2f;
236 std::string str1 =
"Hi World";
238 volatile size_t res = 0;
239 for(
size_t i = 0; i <
loops; ++i ) {
241 std::string s =
jau::cfmt::format(
"format_check: %.2f, %2.2f, %zu, %" PRIu64
", %03d, %10s", fa, fb, sz1, sz2, i1, str1);
242 REQUIRE(format_check_exp == s);
243 res = res + s.size();
247 BENCHMARK(
"fmtX.50 stringstream bench") {
248 float fa = 1.1f, fb = 2.2f;
252 std::string str1 =
"Hi World";
254 volatile size_t res = 0;
255 for(
size_t i = 0; i <
loops; ++i ) {
256 std::ostringstream ss1;
257 ss1 <<
"format_check: "
264 std::string s = ss1.str();
265 REQUIRE(
"format_check: 1.1, 2.2, 1, 2, 3, Hi World" == s);
266 res = res + s.size();
271 BENCHMARK(
"fmtX.60 stdformat bench") {
272 float fa = 1.1f, fb = 2.2f;
276 std::string str1 =
"Hi World";
278 volatile size_t res = 0;
279 for(
size_t i = 0; i <
loops; ++i ) {
280 std::string s = std::format(
"format_040b: {0:.2f}, {1:2.2f}, {3}, {4}, {5:03d}, {6:10s}", fa, fb, sz1, sz2, i1, str1);
281 REQUIRE(format_check_exp == s);
282 res = res + s.size();
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...