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;
46  benchmark::DoNotOptimize(n);
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()) {
55  benchmark::DoNotOptimize(++c);
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()) {
63  const std::string s = absl::FormatCivilTime(c);
64  benchmark::DoNotOptimize(s);
65  }
66 }
67 BENCHMARK(BM_Format);
68 
69 template <typename T>
70 void BM_CivilTimeAbslHash(benchmark::State& state) {
71  const int kSize = 100000;
72  std::vector<T> civil_times(kSize);
73  std::iota(civil_times.begin(), civil_times.end(), T(2018));
74 
75  absl::Hash<T> absl_hasher;
76  while (state.KeepRunningBatch(kSize)) {
77  for (const T civil_time : civil_times) {
78  benchmark::DoNotOptimize(absl_hasher(civil_time));
79  }
80  }
81 }
82 void BM_CivilYearAbslHash(benchmark::State& state) {
83  BM_CivilTimeAbslHash<absl::CivilYear>(state);
84 }
85 void BM_CivilMonthAbslHash(benchmark::State& state) {
86  BM_CivilTimeAbslHash<absl::CivilMonth>(state);
87 }
88 void BM_CivilDayAbslHash(benchmark::State& state) {
89  BM_CivilTimeAbslHash<absl::CivilDay>(state);
90 }
91 void BM_CivilHourAbslHash(benchmark::State& state) {
92  BM_CivilTimeAbslHash<absl::CivilHour>(state);
93 }
94 void BM_CivilMinuteAbslHash(benchmark::State& state) {
95  BM_CivilTimeAbslHash<absl::CivilMinute>(state);
96 }
97 void BM_CivilSecondAbslHash(benchmark::State& state) {
98  BM_CivilTimeAbslHash<absl::CivilSecond>(state);
99 }
100 BENCHMARK(BM_CivilYearAbslHash);
101 BENCHMARK(BM_CivilMonthAbslHash);
102 BENCHMARK(BM_CivilDayAbslHash);
103 BENCHMARK(BM_CivilHourAbslHash);
104 BENCHMARK(BM_CivilMinuteAbslHash);
105 BENCHMARK(BM_CivilSecondAbslHash);
106 
107 } // namespace
time_internal::cctz::diff_t civil_diff_t
Definition: civil_time.h:359
std::string FormatCivilTime(CivilSecond c)
Definition: civil_time.cc:47


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35