1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "android-base/format.h"
18
19 #include <unistd.h>
20
21 #include <limits>
22
23 #include <benchmark/benchmark.h>
24
25 #include "android-base/stringprintf.h"
26
27 using android::base::StringPrintf;
28
29 static pid_t pid = getpid();
30 static int fd = 123;
31
BM_format_fmt_format_ints(benchmark::State & state)32 static void BM_format_fmt_format_ints(benchmark::State& state) {
33 for (auto _ : state) {
34 benchmark::DoNotOptimize(fmt::format("/proc/{}/fd/{}", pid, fd));
35 }
36 }
37 BENCHMARK(BM_format_fmt_format_ints);
38
BM_format_std_format_ints(benchmark::State & state)39 static void BM_format_std_format_ints(benchmark::State& state) {
40 for (auto _ : state) {
41 benchmark::DoNotOptimize(std::format("/proc/{}/fd/{}", pid, fd));
42 }
43 }
44 BENCHMARK(BM_format_std_format_ints);
45
BM_format_StringPrintf_ints(benchmark::State & state)46 static void BM_format_StringPrintf_ints(benchmark::State& state) {
47 for (auto _ : state) {
48 benchmark::DoNotOptimize(StringPrintf("/proc/%d/fd/%d", pid, fd));
49 }
50 }
51 BENCHMARK(BM_format_StringPrintf_ints);
52
BM_format_fmt_format_floats(benchmark::State & state)53 static void BM_format_fmt_format_floats(benchmark::State& state) {
54 for (auto _ : state) {
55 benchmark::DoNotOptimize(fmt::format("{} {} {}", 42.42, std::numeric_limits<float>::min(),
56 std::numeric_limits<float>::max()));
57 }
58 }
59 BENCHMARK(BM_format_fmt_format_floats);
60
BM_format_std_format_floats(benchmark::State & state)61 static void BM_format_std_format_floats(benchmark::State& state) {
62 for (auto _ : state) {
63 benchmark::DoNotOptimize(std::format("{} {} {}", 42.42, std::numeric_limits<float>::min(),
64 std::numeric_limits<float>::max()));
65 }
66 }
67 BENCHMARK(BM_format_std_format_floats);
68
BM_format_StringPrintf_floats(benchmark::State & state)69 static void BM_format_StringPrintf_floats(benchmark::State& state) {
70 for (auto _ : state) {
71 benchmark::DoNotOptimize(StringPrintf("%f %f %f", 42.42, std::numeric_limits<float>::min(),
72 std::numeric_limits<float>::max()));
73 }
74 }
75 BENCHMARK(BM_format_StringPrintf_floats);
76
BM_format_fmt_format_strings(benchmark::State & state)77 static void BM_format_fmt_format_strings(benchmark::State& state) {
78 for (auto _ : state) {
79 benchmark::DoNotOptimize(fmt::format("{} hello there {}", "hi,", "!!"));
80 }
81 }
82 BENCHMARK(BM_format_fmt_format_strings);
83
BM_format_std_format_strings(benchmark::State & state)84 static void BM_format_std_format_strings(benchmark::State& state) {
85 for (auto _ : state) {
86 benchmark::DoNotOptimize(std::format("{} hello there {}", "hi,", "!!"));
87 }
88 }
89 BENCHMARK(BM_format_std_format_strings);
90
BM_format_StringPrintf_strings(benchmark::State & state)91 static void BM_format_StringPrintf_strings(benchmark::State& state) {
92 for (auto _ : state) {
93 benchmark::DoNotOptimize(StringPrintf("%s hello there %s", "hi,", "!!"));
94 }
95 }
96 BENCHMARK(BM_format_StringPrintf_strings);
97
98 // Run the benchmark
99 BENCHMARK_MAIN();
100