char_map_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/strings/internal/char_map.h"
00016 
00017 #include <cstdint>
00018 
00019 #include "benchmark/benchmark.h"
00020 
00021 namespace {
00022 
00023 absl::strings_internal::Charmap MakeBenchmarkMap() {
00024   absl::strings_internal::Charmap m;
00025   uint32_t x[] = {0x0, 0x1, 0x2, 0x3, 0xf, 0xe, 0xd, 0xc};
00026   for (uint32_t& t : x) t *= static_cast<uint32_t>(0x11111111UL);
00027   for (uint32_t i = 0; i < 256; ++i) {
00028     if ((x[i / 32] >> (i % 32)) & 1)
00029       m = m | absl::strings_internal::Charmap::Char(i);
00030   }
00031   return m;
00032 }
00033 
00034 // Micro-benchmark for Charmap::contains.
00035 void BM_Contains(benchmark::State& state) {
00036   // Loop-body replicated 10 times to increase time per iteration.
00037   // Argument continuously changed to avoid generating common subexpressions.
00038   const absl::strings_internal::Charmap benchmark_map = MakeBenchmarkMap();
00039   unsigned char c = 0;
00040   int ops = 0;
00041   for (auto _ : state) {
00042     ops += benchmark_map.contains(c++);
00043     ops += benchmark_map.contains(c++);
00044     ops += benchmark_map.contains(c++);
00045     ops += benchmark_map.contains(c++);
00046     ops += benchmark_map.contains(c++);
00047     ops += benchmark_map.contains(c++);
00048     ops += benchmark_map.contains(c++);
00049     ops += benchmark_map.contains(c++);
00050     ops += benchmark_map.contains(c++);
00051     ops += benchmark_map.contains(c++);
00052   }
00053   benchmark::DoNotOptimize(ops);
00054 }
00055 BENCHMARK(BM_Contains);
00056 
00057 // We don't bother benchmarking Charmap::IsZero or Charmap::IntersectsWith;
00058 // their running time is data-dependent and it is not worth characterizing
00059 // "typical" data.
00060 
00061 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14