bits_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2022 The Abseil Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <cstdint>
16 #include <vector>
17 
18 #include "benchmark/benchmark.h"
19 #include "absl/base/optimization.h"
20 #include "absl/numeric/bits.h"
21 #include "absl/random/random.h"
22 
23 namespace absl {
24 namespace {
25 
26 template <typename T>
27 static void BM_bitwidth(benchmark::State& state) {
28  const int count = state.range(0);
29 
30  absl::BitGen rng;
31  std::vector<T> values;
32  values.reserve(count);
33  for (int i = 0; i < count; ++i) {
34  values.push_back(absl::Uniform<T>(rng, 0, std::numeric_limits<T>::max()));
35  }
36 
37  while (state.KeepRunningBatch(count)) {
38  for (int i = 0; i < count; ++i) {
40  }
41  }
42 }
43 BENCHMARK_TEMPLATE(BM_bitwidth, uint8_t)->Range(1, 1 << 20);
44 BENCHMARK_TEMPLATE(BM_bitwidth, uint16_t)->Range(1, 1 << 20);
45 BENCHMARK_TEMPLATE(BM_bitwidth, uint32_t)->Range(1, 1 << 20);
46 BENCHMARK_TEMPLATE(BM_bitwidth, uint64_t)->Range(1, 1 << 20);
47 
48 template <typename T>
49 static void BM_bitwidth_nonzero(benchmark::State& state) {
50  const int count = state.range(0);
51 
52  absl::BitGen rng;
53  std::vector<T> values;
54  values.reserve(count);
55  for (int i = 0; i < count; ++i) {
56  values.push_back(absl::Uniform<T>(rng, 1, std::numeric_limits<T>::max()));
57  }
58 
59  while (state.KeepRunningBatch(count)) {
60  for (int i = 0; i < count; ++i) {
61  const T value = values[i];
62  ABSL_ASSUME(value > 0);
64  }
65  }
66 }
67 BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint8_t)->Range(1, 1 << 20);
68 BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint16_t)->Range(1, 1 << 20);
69 BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint32_t)->Range(1, 1 << 20);
70 BENCHMARK_TEMPLATE(BM_bitwidth_nonzero, uint64_t)->Range(1, 1 << 20);
71 
72 } // namespace
73 } // namespace absl
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
T
#define T(upbtypeconst, upbtype, ctype, default_value)
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
BENCHMARK_TEMPLATE
#define BENCHMARK_TEMPLATE(n, a)
Definition: benchmark/include/benchmark/benchmark.h:1231
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
value
const char * value
Definition: hpack_parser_table.cc:165
ABSL_ASSUME
#define ABSL_ASSUME(cond)
Definition: abseil-cpp/absl/base/optimization.h:209
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
absl::random_internal::NonsecureURBGBase< random_internal::randen_engine< uint64_t > >
values
std::array< int64_t, Size > values
Definition: abseil-cpp/absl/container/btree_benchmark.cc:608
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:38