xref: /aosp_15_r20/system/libbase/format_benchmark.cpp (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/format.h"
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <limits>
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker #include <benchmark/benchmark.h>
22*8f0ba417SAndroid Build Coastguard Worker 
23*8f0ba417SAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
24*8f0ba417SAndroid Build Coastguard Worker 
25*8f0ba417SAndroid Build Coastguard Worker using android::base::StringPrintf;
26*8f0ba417SAndroid Build Coastguard Worker 
BenchmarkFormatInt(benchmark::State & state)27*8f0ba417SAndroid Build Coastguard Worker static void BenchmarkFormatInt(benchmark::State& state) {
28*8f0ba417SAndroid Build Coastguard Worker   for (auto _ : state) {
29*8f0ba417SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(fmt::format("{} {} {}", 42, std::numeric_limits<int>::min(),
30*8f0ba417SAndroid Build Coastguard Worker                                          std::numeric_limits<int>::max()));
31*8f0ba417SAndroid Build Coastguard Worker   }
32*8f0ba417SAndroid Build Coastguard Worker }
33*8f0ba417SAndroid Build Coastguard Worker 
34*8f0ba417SAndroid Build Coastguard Worker BENCHMARK(BenchmarkFormatInt);
35*8f0ba417SAndroid Build Coastguard Worker 
BenchmarkStringPrintfInt(benchmark::State & state)36*8f0ba417SAndroid Build Coastguard Worker static void BenchmarkStringPrintfInt(benchmark::State& state) {
37*8f0ba417SAndroid Build Coastguard Worker   for (auto _ : state) {
38*8f0ba417SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(StringPrintf("%d %d %d", 42, std::numeric_limits<int>::min(),
39*8f0ba417SAndroid Build Coastguard Worker                                           std::numeric_limits<int>::max()));
40*8f0ba417SAndroid Build Coastguard Worker   }
41*8f0ba417SAndroid Build Coastguard Worker }
42*8f0ba417SAndroid Build Coastguard Worker 
43*8f0ba417SAndroid Build Coastguard Worker BENCHMARK(BenchmarkStringPrintfInt);
44*8f0ba417SAndroid Build Coastguard Worker 
BenchmarkFormatFloat(benchmark::State & state)45*8f0ba417SAndroid Build Coastguard Worker static void BenchmarkFormatFloat(benchmark::State& state) {
46*8f0ba417SAndroid Build Coastguard Worker   for (auto _ : state) {
47*8f0ba417SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(fmt::format("{} {} {}", 42.42, std::numeric_limits<float>::min(),
48*8f0ba417SAndroid Build Coastguard Worker                                          std::numeric_limits<float>::max()));
49*8f0ba417SAndroid Build Coastguard Worker   }
50*8f0ba417SAndroid Build Coastguard Worker }
51*8f0ba417SAndroid Build Coastguard Worker 
52*8f0ba417SAndroid Build Coastguard Worker BENCHMARK(BenchmarkFormatFloat);
53*8f0ba417SAndroid Build Coastguard Worker 
BenchmarkStringPrintfFloat(benchmark::State & state)54*8f0ba417SAndroid Build Coastguard Worker static void BenchmarkStringPrintfFloat(benchmark::State& state) {
55*8f0ba417SAndroid Build Coastguard Worker   for (auto _ : state) {
56*8f0ba417SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(StringPrintf("%f %f %f", 42.42, std::numeric_limits<float>::min(),
57*8f0ba417SAndroid Build Coastguard Worker                                           std::numeric_limits<float>::max()));
58*8f0ba417SAndroid Build Coastguard Worker   }
59*8f0ba417SAndroid Build Coastguard Worker }
60*8f0ba417SAndroid Build Coastguard Worker 
61*8f0ba417SAndroid Build Coastguard Worker BENCHMARK(BenchmarkStringPrintfFloat);
62*8f0ba417SAndroid Build Coastguard Worker 
BenchmarkFormatStrings(benchmark::State & state)63*8f0ba417SAndroid Build Coastguard Worker static void BenchmarkFormatStrings(benchmark::State& state) {
64*8f0ba417SAndroid Build Coastguard Worker   for (auto _ : state) {
65*8f0ba417SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(fmt::format("{} hello there {}", "hi,", "!!"));
66*8f0ba417SAndroid Build Coastguard Worker   }
67*8f0ba417SAndroid Build Coastguard Worker }
68*8f0ba417SAndroid Build Coastguard Worker 
69*8f0ba417SAndroid Build Coastguard Worker BENCHMARK(BenchmarkFormatStrings);
70*8f0ba417SAndroid Build Coastguard Worker 
BenchmarkStringPrintfStrings(benchmark::State & state)71*8f0ba417SAndroid Build Coastguard Worker static void BenchmarkStringPrintfStrings(benchmark::State& state) {
72*8f0ba417SAndroid Build Coastguard Worker   for (auto _ : state) {
73*8f0ba417SAndroid Build Coastguard Worker     benchmark::DoNotOptimize(StringPrintf("%s hello there %s", "hi,", "!!"));
74*8f0ba417SAndroid Build Coastguard Worker   }
75*8f0ba417SAndroid Build Coastguard Worker }
76*8f0ba417SAndroid Build Coastguard Worker 
77*8f0ba417SAndroid Build Coastguard Worker BENCHMARK(BenchmarkStringPrintfStrings);
78*8f0ba417SAndroid Build Coastguard Worker 
79*8f0ba417SAndroid Build Coastguard Worker // Run the benchmark
80*8f0ba417SAndroid Build Coastguard Worker BENCHMARK_MAIN();
81