30#include <jau/test/catch2_ext.hpp>
38using namespace std::literals;
47 static constexpr const size_t char32buf_maxlen = 32;
48 char buf_[char32buf_maxlen];
50 const char *
const d_end_num = d + char32buf_maxlen;
55 }
while( v && d < d_end_num);
59 static constexpr const size_t char32buf_maxlen = 32;
60 char buf_[char32buf_maxlen];
62 const char *
const d_end_num = d + char32buf_maxlen;
64 *(d++) =
char(
'0' + (v % 10_u64));
66 }
while( v && d < d_end_num);
67 return uint32_t(d - buf_);
70TEST_CASE(
"fast_log_benchmark_digits10",
"[benchmark][jau][math][log]") {
71 const size_t loops = 1000;
72 WARN(
"Benchmark with " + std::to_string(
loops) +
" loops");
75 const double log2_10 = std::log2<uint64_t>(10);
76 const uint64_t i1 = std::numeric_limits<uint64_t>::max();
77 const uint32_t i1_d10 = 20;
79 BENCHMARK(
"O(n) loop0 bench") {
80 volatile size_t res = 0;
81 for(
size_t i = 0; i <
loops; ++i ) {
88 BENCHMARK(
"O(n) loop1 bench") {
89 volatile size_t res = 0;
90 for(
size_t i = 0; i <
loops; ++i ) {
97 BENCHMARK(
"log10(x) bench") {
98 volatile size_t res = 0;
99 for(
size_t i = 0; i <
loops; ++i ) {
100 uint32_t l = 1 +
static_cast<uint32_t
>(std::log10<uint64_t>(i1));
101 REQUIRE(i1_d10 == l);
106 BENCHMARK(
"log2(x)/log2(10) bench") {
107 volatile size_t res = 0;
108 for(
size_t i = 0; i <
loops; ++i ) {
109 uint32_t l = 1 +
static_cast<uint32_t
>(std::log2<uint64_t>(i1) / log2_10);
110 REQUIRE(i1_d10 == l);
117TEST_CASE(
"jau_cfmt_benchmark_append_integral00",
"[benchmark][jau][std::string][format_string]") {
118 const size_t loops = 1000;
119 WARN(
"Benchmark with " + std::to_string(
loops) +
" loops");
122 const uint64_t i1 = std::numeric_limits<uint64_t>::max();
123 static constexpr const char *format_check_exp =
"18446744073709551615";
128 std::cout <<
"flags: " << o1 <<
"\n";
135 REQUIRE(format_check_exp == s);
138 BENCHMARK(
"append_integral rsrved bench") {
139 volatile size_t res = 0;
140 for(
size_t i = 0; i <
loops; ++i ) {
145 REQUIRE(format_check_exp == s);
146 res = res + s.size();
151 BENCHMARK(
"snprintf rsrved bench") {
152 volatile size_t res = 0;
153 for(
size_t i = 0; i <
loops; ++i ) {
158 size_t nchars = std::snprintf(&s[0], bsz,
"%zu", i1);
162 REQUIRE(format_check_exp == s);
169TEST_CASE(
"jau_cfmt_benchmark_append_integral01",
"[benchmark][jau][std::string][format_string]") {
170 const size_t loops = 1000;
171 WARN(
"Benchmark with " + std::to_string(
loops) +
" loops");
174 const uint64_t i1 = std::numeric_limits<uint64_t>::max();
175 static constexpr const char *format_check_exp1 =
" 0000000018'446'744'073'709'551'615";
176 static constexpr const char *format_check_exp0 =
" 0000000000000018446744073709551615";
183 std::cout <<
"flags: " << o1 <<
"\n";
190 REQUIRE(format_check_exp1 == s);
193 BENCHMARK(
"append_integral rsrved bench") {
194 volatile size_t res = 0;
195 for(
size_t i = 0; i <
loops; ++i ) {
200 REQUIRE(format_check_exp1 == s);
201 res = res + s.size();
206 BENCHMARK(
"snprintf rsrved bench") {
207 volatile size_t res = 0;
208 for(
size_t i = 0; i <
loops; ++i ) {
213 size_t nchars = std::snprintf(&s[0], bsz,
"%38.34zu", i1);
217 REQUIRE(format_check_exp0 == s);
constexpr const size_t default_string_capacity
Default string reserved capacity w/o EOS (511)
@ z
size_t or ssize_t integer
void append_integral(std::string &dest, const size_t dest_maxlen, uint64_t v, const bool negative, const FormatOpts &opts, const bool inject_dot=false)
static uint32_t digits10_loop0(uint64_t v) noexcept
Execute with test_stringfmt_perf_impl --perf-analysis
static uint32_t digits10_loop1(uint64_t v) noexcept
TEST_CASE("fast_log_benchmark_digits10", "[benchmark][jau][math][log]")