abseil-cpp/absl/time/civil_time_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2018 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 "absl/time/civil_time.h"
16 
17 #include <numeric>
18 #include <vector>
19 
20 #include "absl/hash/hash.h"
21 #include "benchmark/benchmark.h"
22 
23 namespace {
24 
25 // Run on (12 X 3492 MHz CPUs); 2018-11-05T13:44:29.814239103-08:00
26 // CPU: Intel Haswell with HyperThreading (6 cores) dL1:32KB dL2:256KB dL3:15MB
27 // Benchmark Time(ns) CPU(ns) Iterations
28 // ----------------------------------------------------------------
29 // BM_Difference_Days 14.5 14.5 48531105
30 // BM_Step_Days 12.6 12.6 54876006
31 // BM_Format 587 587 1000000
32 // BM_Parse 692 692 1000000
33 // BM_RoundTripFormatParse 1309 1309 532075
34 // BM_CivilYearAbslHash 0.710 0.710 976400000
35 // BM_CivilMonthAbslHash 1.13 1.13 619500000
36 // BM_CivilDayAbslHash 1.70 1.70 426000000
37 // BM_CivilHourAbslHash 2.45 2.45 287600000
38 // BM_CivilMinuteAbslHash 3.21 3.21 226200000
39 // BM_CivilSecondAbslHash 4.10 4.10 171800000
40 
41 void BM_Difference_Days(benchmark::State& state) {
42  const absl::CivilDay c(2014, 8, 22);
43  const absl::CivilDay epoch(1970, 1, 1);
44  while (state.KeepRunning()) {
45  const absl::civil_diff_t n = c - epoch;
47  }
48 }
49 BENCHMARK(BM_Difference_Days);
50 
51 void BM_Step_Days(benchmark::State& state) {
52  const absl::CivilDay kStart(2014, 8, 22);
53  absl::CivilDay c = kStart;
54  while (state.KeepRunning()) {
56  }
57 }
58 BENCHMARK(BM_Step_Days);
59 
60 void BM_Format(benchmark::State& state) {
61  const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
62  while (state.KeepRunning()) {
65  }
66 }
67 BENCHMARK(BM_Format);
68 
69 void BM_Parse(benchmark::State& state) {
70  const std::string f = "2014-01-02T03:04:05";
72  while (state.KeepRunning()) {
73  const bool b = absl::ParseCivilTime(f, &c);
75  }
76 }
77 BENCHMARK(BM_Parse);
78 
79 void BM_RoundTripFormatParse(benchmark::State& state) {
80  const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
82  while (state.KeepRunning()) {
85  }
86 }
87 BENCHMARK(BM_RoundTripFormatParse);
88 
89 template <typename T>
90 void BM_CivilTimeAbslHash(benchmark::State& state) {
91  const int kSize = 100000;
92  std::vector<T> civil_times(kSize);
93  std::iota(civil_times.begin(), civil_times.end(), T(2018));
94 
95  absl::Hash<T> absl_hasher;
96  while (state.KeepRunningBatch(kSize)) {
97  for (const T civil_time : civil_times) {
98  benchmark::DoNotOptimize(absl_hasher(civil_time));
99  }
100  }
101 }
102 void BM_CivilYearAbslHash(benchmark::State& state) {
103  BM_CivilTimeAbslHash<absl::CivilYear>(state);
104 }
105 void BM_CivilMonthAbslHash(benchmark::State& state) {
106  BM_CivilTimeAbslHash<absl::CivilMonth>(state);
107 }
108 void BM_CivilDayAbslHash(benchmark::State& state) {
109  BM_CivilTimeAbslHash<absl::CivilDay>(state);
110 }
111 void BM_CivilHourAbslHash(benchmark::State& state) {
112  BM_CivilTimeAbslHash<absl::CivilHour>(state);
113 }
114 void BM_CivilMinuteAbslHash(benchmark::State& state) {
115  BM_CivilTimeAbslHash<absl::CivilMinute>(state);
116 }
117 void BM_CivilSecondAbslHash(benchmark::State& state) {
118  BM_CivilTimeAbslHash<absl::CivilSecond>(state);
119 }
120 BENCHMARK(BM_CivilYearAbslHash);
121 BENCHMARK(BM_CivilMonthAbslHash);
122 BENCHMARK(BM_CivilDayAbslHash);
123 BENCHMARK(BM_CivilHourAbslHash);
124 BENCHMARK(BM_CivilMinuteAbslHash);
125 BENCHMARK(BM_CivilSecondAbslHash);
126 
127 } // namespace
kSize
static constexpr Tag kSize
Definition: protobuf/src/google/protobuf/descriptor.cc:873
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
absl::hash_internal::Hash
Definition: abseil-cpp/absl/hash/internal/hash.h:1221
absl::time_internal::cctz::detail::civil_time
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time_detail.h:376
absl::FormatCivilTime
std::string FormatCivilTime(CivilSecond c)
Definition: abseil-cpp/absl/time/civil_time.cc:98
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::DoNotOptimize
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
Definition: benchmark/include/benchmark/benchmark.h:375
absl::FormatConversionChar::s
@ s
T
#define T(upbtypeconst, upbtype, ctype, default_value)
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
absl::ParseCivilTime
bool ParseCivilTime(string_view s, CivilSecond *c)
Definition: abseil-cpp/absl/time/civil_time.cc:111
epoch
int64_t epoch(int year, int yday, int hour, int min, int sec)
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:10172
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
absl::civil_diff_t
time_internal::cctz::diff_t civil_diff_t
Definition: abseil-cpp/absl/time/civil_time.h:360
BENCHMARK
#define BENCHMARK(n)
Definition: benchmark/include/benchmark/benchmark.h:1170


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:54