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 <cstddef>
18 #include <numeric>
19 #include <string>
20 #include <vector>
21
22 #include "absl/hash/hash.h"
23 #include "benchmark/benchmark.h"
24
25 namespace {
26
27 // Run on (12 X 3492 MHz CPUs); 2018-11-05T13:44:29.814239103-08:00
28 // CPU: Intel Haswell with HyperThreading (6 cores) dL1:32KB dL2:256KB dL3:15MB
29 // Benchmark Time(ns) CPU(ns) Iterations
30 // ----------------------------------------------------------------
31 // BM_Difference_Days 14.5 14.5 48531105
32 // BM_Step_Days 12.6 12.6 54876006
33 // BM_Format 587 587 1000000
34 // BM_Parse 692 692 1000000
35 // BM_RoundTripFormatParse 1309 1309 532075
36 // BM_CivilYearAbslHash 0.710 0.710 976400000
37 // BM_CivilMonthAbslHash 1.13 1.13 619500000
38 // BM_CivilDayAbslHash 1.70 1.70 426000000
39 // BM_CivilHourAbslHash 2.45 2.45 287600000
40 // BM_CivilMinuteAbslHash 3.21 3.21 226200000
41 // BM_CivilSecondAbslHash 4.10 4.10 171800000
42
BM_Difference_Days(benchmark::State & state)43 void BM_Difference_Days(benchmark::State& state) {
44 const absl::CivilDay c(2014, 8, 22);
45 const absl::CivilDay epoch(1970, 1, 1);
46 while (state.KeepRunning()) {
47 absl::civil_diff_t n = c - epoch;
48 benchmark::DoNotOptimize(n);
49 }
50 }
51 BENCHMARK(BM_Difference_Days);
52
BM_Step_Days(benchmark::State & state)53 void BM_Step_Days(benchmark::State& state) {
54 const absl::CivilDay kStart(2014, 8, 22);
55 absl::CivilDay c = kStart;
56 while (state.KeepRunning()) {
57 benchmark::DoNotOptimize(++c);
58 }
59 }
60 BENCHMARK(BM_Step_Days);
61
BM_Format(benchmark::State & state)62 void BM_Format(benchmark::State& state) {
63 const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
64 while (state.KeepRunning()) {
65 std::string s = absl::FormatCivilTime(c);
66 benchmark::DoNotOptimize(s);
67 }
68 }
69 BENCHMARK(BM_Format);
70
BM_Parse(benchmark::State & state)71 void BM_Parse(benchmark::State& state) {
72 const std::string f = "2014-01-02T03:04:05";
73 absl::CivilSecond c;
74 while (state.KeepRunning()) {
75 bool b = absl::ParseCivilTime(f, &c);
76 benchmark::DoNotOptimize(b);
77 }
78 }
79 BENCHMARK(BM_Parse);
80
BM_RoundTripFormatParse(benchmark::State & state)81 void BM_RoundTripFormatParse(benchmark::State& state) {
82 const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
83 absl::CivilSecond out;
84 while (state.KeepRunning()) {
85 bool b = absl::ParseCivilTime(absl::FormatCivilTime(c), &out);
86 benchmark::DoNotOptimize(b);
87 }
88 }
89 BENCHMARK(BM_RoundTripFormatParse);
90
91 template <typename T>
BM_CivilTimeAbslHash(benchmark::State & state)92 void BM_CivilTimeAbslHash(benchmark::State& state) {
93 const int kSize = 100000;
94 std::vector<T> civil_times(kSize);
95 std::iota(civil_times.begin(), civil_times.end(), T(2018));
96
97 absl::Hash<T> absl_hasher;
98 while (state.KeepRunningBatch(kSize)) {
99 for (const T civil_time : civil_times) {
100 size_t hash = absl_hasher(civil_time);
101 benchmark::DoNotOptimize(hash);
102 }
103 }
104 }
BM_CivilYearAbslHash(benchmark::State & state)105 void BM_CivilYearAbslHash(benchmark::State& state) {
106 BM_CivilTimeAbslHash<absl::CivilYear>(state);
107 }
BM_CivilMonthAbslHash(benchmark::State & state)108 void BM_CivilMonthAbslHash(benchmark::State& state) {
109 BM_CivilTimeAbslHash<absl::CivilMonth>(state);
110 }
BM_CivilDayAbslHash(benchmark::State & state)111 void BM_CivilDayAbslHash(benchmark::State& state) {
112 BM_CivilTimeAbslHash<absl::CivilDay>(state);
113 }
BM_CivilHourAbslHash(benchmark::State & state)114 void BM_CivilHourAbslHash(benchmark::State& state) {
115 BM_CivilTimeAbslHash<absl::CivilHour>(state);
116 }
BM_CivilMinuteAbslHash(benchmark::State & state)117 void BM_CivilMinuteAbslHash(benchmark::State& state) {
118 BM_CivilTimeAbslHash<absl::CivilMinute>(state);
119 }
BM_CivilSecondAbslHash(benchmark::State & state)120 void BM_CivilSecondAbslHash(benchmark::State& state) {
121 BM_CivilTimeAbslHash<absl::CivilSecond>(state);
122 }
123 BENCHMARK(BM_CivilYearAbslHash);
124 BENCHMARK(BM_CivilMonthAbslHash);
125 BENCHMARK(BM_CivilDayAbslHash);
126 BENCHMARK(BM_CivilHourAbslHash);
127 BENCHMARK(BM_CivilMinuteAbslHash);
128 BENCHMARK(BM_CivilSecondAbslHash);
129
130 } // namespace
131