damerau_levenshtein_distance_benchmark.cc (1796B)
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 <string> 16 17 #include "absl/strings/internal/damerau_levenshtein_distance.h" 18 #include "benchmark/benchmark.h" 19 20 namespace { 21 22 std::string MakeTestString(int desired_length, int num_edits) { 23 std::string test(desired_length, 'x'); 24 for (int i = 0; (i < num_edits) && (i * 8 < desired_length); ++i) { 25 test[i * 8] = 'y'; 26 } 27 return test; 28 } 29 30 void BenchmarkArgs(benchmark::internal::Benchmark* benchmark) { 31 // Each column is 8 bytes. 32 const auto string_size = {1, 8, 64, 100}; 33 const auto num_edits = {1, 2, 16, 16, 64, 80}; 34 const auto distance_cap = {1, 2, 3, 16, 64, 80}; 35 for (const int s : string_size) { 36 for (const int n : num_edits) { 37 for (const int d : distance_cap) { 38 if (n > s) continue; 39 benchmark->Args({s, n, d}); 40 } 41 } 42 } 43 } 44 45 using absl::strings_internal::CappedDamerauLevenshteinDistance; 46 void BM_Distance(benchmark::State& state) { 47 std::string s1 = MakeTestString(state.range(0), 0); 48 std::string s2 = MakeTestString(state.range(0), state.range(1)); 49 const size_t cap = state.range(2); 50 for (auto _ : state) { 51 CappedDamerauLevenshteinDistance(s1, s2, cap); 52 } 53 } 54 BENCHMARK(BM_Distance)->Apply(BenchmarkArgs); 55 56 } // namespace