18 #include <type_traits> 21 #include "benchmark/benchmark.h" 28 void BM_FastIntToBuffer(benchmark::State& state) {
29 const int inc = state.range(0);
33 typename std::make_unsigned<T>::type x = 0;
34 for (
auto _ : state) {
39 BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int32_t)->Range(0, 1 << 15);
40 BENCHMARK_TEMPLATE(BM_FastIntToBuffer, int64_t)->Range(0, 1 << 30);
44 int64_t RepeatedSevens(
int num_digits,
int base) {
47 while (--num_digits) num = base * num + 7;
51 void BM_safe_strto32_string(benchmark::State& state) {
52 const int digits = state.range(0);
53 const int base = state.range(1);
54 std::string str(digits,
'7');
56 for (
auto _ : state) {
57 benchmark::DoNotOptimize(
62 BENCHMARK(BM_safe_strto32_string)
78 void BM_safe_strto64_string(benchmark::State& state) {
79 const int digits = state.range(0);
80 const int base = state.range(1);
81 std::string str(digits,
'7');
83 for (
auto _ : state) {
84 benchmark::DoNotOptimize(
89 BENCHMARK(BM_safe_strto64_string)
106 void BM_safe_strtou32_string(benchmark::State& state) {
107 const int digits = state.range(0);
108 const int base = state.range(1);
109 std::string str(digits,
'7');
111 for (
auto _ : state) {
112 benchmark::DoNotOptimize(
117 BENCHMARK(BM_safe_strtou32_string)
133 void BM_safe_strtou64_string(benchmark::State& state) {
134 const int digits = state.range(0);
135 const int base = state.range(1);
136 std::string str(digits,
'7');
138 for (
auto _ : state) {
139 benchmark::DoNotOptimize(
144 BENCHMARK(BM_safe_strtou64_string)
164 std::vector<std::string> MakeFloatStrings(
int num_strings,
int num_digits) {
167 std::minstd_rand0 rng(1);
168 std::uniform_int_distribution<int> random_digit(
'0',
'9');
170 std::vector<std::string> float_strings(num_strings);
171 for (std::string& s : float_strings) {
172 s.reserve(2 * num_digits + 1);
173 for (
int i = 0;
i < num_digits; ++
i) {
174 s.push_back(static_cast<char>(random_digit(rng)));
177 for (
int i = 0;
i < num_digits; ++
i) {
178 s.push_back(static_cast<char>(random_digit(rng)));
181 return float_strings;
184 template <
typename StringType>
185 StringType GetStringAs(
const std::string& s) {
189 const char* GetStringAs<const char*>(
const std::string& s) {
193 template <
typename StringType>
194 std::vector<StringType> GetStringsAs(
const std::vector<std::string>& strings) {
195 std::vector<StringType> result;
196 result.reserve(strings.size());
197 for (
const std::string& s : strings) {
198 result.push_back(GetStringAs<StringType>(s));
203 template <
typename T>
204 void BM_SimpleAtof(benchmark::State& state) {
205 const int num_strings = state.range(0);
206 const int num_digits = state.range(1);
207 std::vector<std::string> backing_strings =
208 MakeFloatStrings(num_strings, num_digits);
209 std::vector<T> inputs = GetStringsAs<T>(backing_strings);
211 for (
auto _ : state) {
212 for (
const T& input : inputs) {
222 BENCHMARK_TEMPLATE(BM_SimpleAtof,
const char*)
227 BENCHMARK_TEMPLATE(BM_SimpleAtof, std::string)
233 template <
typename T>
234 void BM_SimpleAtod(benchmark::State& state) {
235 const int num_strings = state.range(0);
236 const int num_digits = state.range(1);
237 std::vector<std::string> backing_strings =
238 MakeFloatStrings(num_strings, num_digits);
239 std::vector<T> inputs = GetStringsAs<T>(backing_strings);
241 for (
auto _ : state) {
242 for (
const T& input : inputs) {
252 BENCHMARK_TEMPLATE(BM_SimpleAtod,
const char*)
257 BENCHMARK_TEMPLATE(BM_SimpleAtod, std::string)
bool safe_strtou64_base(absl::string_view text, uint64_t *value, int base)
char * FastIntToBuffer(int32_t, char *)
bool safe_strtou32_base(absl::string_view text, uint32_t *value, int base)
bool SimpleAtod(absl::string_view str, double *out)
bool SimpleAtof(absl::string_view str, float *out)
bool safe_strto32_base(absl::string_view text, int32_t *value, int base)
#define ABSL_RAW_CHECK(condition, message)
bool safe_strto64_base(absl::string_view text, int64_t *value, int base)
static const int kFastToBufferSize