• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
BM_Difference_Days(benchmark::State & state)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 
BM_Step_Days(benchmark::State & state)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 
BM_Format(benchmark::State & state)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 
BM_Parse(benchmark::State & state)69 void BM_Parse(benchmark::State& state) {
70   const std::string f = "2014-01-02T03:04:05";
71   absl::CivilSecond c;
72   while (state.KeepRunning()) {
73     const bool b = absl::ParseCivilTime(f, &c);
74     benchmark::DoNotOptimize(b);
75   }
76 }
77 BENCHMARK(BM_Parse);
78 
BM_RoundTripFormatParse(benchmark::State & state)79 void BM_RoundTripFormatParse(benchmark::State& state) {
80   const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
81   absl::CivilSecond out;
82   while (state.KeepRunning()) {
83     const bool b = absl::ParseCivilTime(absl::FormatCivilTime(c), &out);
84     benchmark::DoNotOptimize(b);
85   }
86 }
87 BENCHMARK(BM_RoundTripFormatParse);
88 
89 template <typename T>
BM_CivilTimeAbslHash(benchmark::State & state)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 }
BM_CivilYearAbslHash(benchmark::State & state)102 void BM_CivilYearAbslHash(benchmark::State& state) {
103   BM_CivilTimeAbslHash<absl::CivilYear>(state);
104 }
BM_CivilMonthAbslHash(benchmark::State & state)105 void BM_CivilMonthAbslHash(benchmark::State& state) {
106   BM_CivilTimeAbslHash<absl::CivilMonth>(state);
107 }
BM_CivilDayAbslHash(benchmark::State & state)108 void BM_CivilDayAbslHash(benchmark::State& state) {
109   BM_CivilTimeAbslHash<absl::CivilDay>(state);
110 }
BM_CivilHourAbslHash(benchmark::State & state)111 void BM_CivilHourAbslHash(benchmark::State& state) {
112   BM_CivilTimeAbslHash<absl::CivilHour>(state);
113 }
BM_CivilMinuteAbslHash(benchmark::State & state)114 void BM_CivilMinuteAbslHash(benchmark::State& state) {
115   BM_CivilTimeAbslHash<absl::CivilMinute>(state);
116 }
BM_CivilSecondAbslHash(benchmark::State & state)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
128