str_join_benchmark.cc
Go to the documentation of this file.
00001 //
00002 // Copyright 2018 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 
00016 #include "absl/strings/str_join.h"
00017 
00018 #include <string>
00019 #include <vector>
00020 #include <utility>
00021 
00022 #include "benchmark/benchmark.h"
00023 
00024 namespace {
00025 
00026 void BM_Join2_Strings(benchmark::State& state) {
00027   const int string_len = state.range(0);
00028   const int num_strings = state.range(1);
00029   const std::string s(string_len, 'x');
00030   const std::vector<std::string> v(num_strings, s);
00031   for (auto _ : state) {
00032     std::string s = absl::StrJoin(v, "-");
00033     benchmark::DoNotOptimize(s);
00034   }
00035 }
00036 BENCHMARK(BM_Join2_Strings)
00037     ->ArgPair(1 << 0, 1 << 3)
00038     ->ArgPair(1 << 10, 1 << 3)
00039     ->ArgPair(1 << 13, 1 << 3)
00040     ->ArgPair(1 << 0, 1 << 10)
00041     ->ArgPair(1 << 10, 1 << 10)
00042     ->ArgPair(1 << 13, 1 << 10)
00043     ->ArgPair(1 << 0, 1 << 13)
00044     ->ArgPair(1 << 10, 1 << 13)
00045     ->ArgPair(1 << 13, 1 << 13);
00046 
00047 void BM_Join2_Ints(benchmark::State& state) {
00048   const int num_ints = state.range(0);
00049   const std::vector<int> v(num_ints, 42);
00050   for (auto _ : state) {
00051     std::string s = absl::StrJoin(v, "-");
00052     benchmark::DoNotOptimize(s);
00053   }
00054 }
00055 BENCHMARK(BM_Join2_Ints)->Range(0, 1 << 13);
00056 
00057 void BM_Join2_KeysAndValues(benchmark::State& state) {
00058   const int string_len = state.range(0);
00059   const int num_pairs = state.range(1);
00060   const std::string s(string_len, 'x');
00061   const std::vector<std::pair<std::string, int>> v(num_pairs,
00062                                                    std::make_pair(s, 42));
00063   for (auto _ : state) {
00064     std::string s = absl::StrJoin(v, ",", absl::PairFormatter("="));
00065     benchmark::DoNotOptimize(s);
00066   }
00067 }
00068 BENCHMARK(BM_Join2_KeysAndValues)
00069     ->ArgPair(1 << 0, 1 << 3)
00070     ->ArgPair(1 << 10, 1 << 3)
00071     ->ArgPair(1 << 13, 1 << 3)
00072     ->ArgPair(1 << 0, 1 << 10)
00073     ->ArgPair(1 << 10, 1 << 10)
00074     ->ArgPair(1 << 13, 1 << 10)
00075     ->ArgPair(1 << 0, 1 << 13)
00076     ->ArgPair(1 << 10, 1 << 13)
00077     ->ArgPair(1 << 13, 1 << 13);
00078 
00079 void BM_JoinStreamable(benchmark::State& state) {
00080   const int string_len = state.range(0);
00081   const int num_strings = state.range(1);
00082   const std::vector<std::string> v(num_strings, std::string(string_len, 'x'));
00083   for (auto _ : state) {
00084     std::string s = absl::StrJoin(v, "", absl::StreamFormatter());
00085     benchmark::DoNotOptimize(s);
00086   }
00087 }
00088 BENCHMARK(BM_JoinStreamable)
00089     ->ArgPair(0, 0)
00090     ->ArgPair(16, 1)
00091     ->ArgPair(256, 1)
00092     ->ArgPair(16, 16)
00093     ->ArgPair(256, 16)
00094     ->ArgPair(16, 256)
00095     ->ArgPair(256, 256);
00096 
00097 }  // namespace


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