format_benchmark.cc
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // https://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include <cstddef>
15 #include <string>
16 
18 #include "absl/time/time.h"
19 #include "benchmark/benchmark.h"
20 
21 namespace {
22 
23 namespace {
24 const char* const kFormats[] = {
25  absl::RFC1123_full, // 0
27  absl::RFC3339_full, // 2
28  absl::RFC3339_sec, // 3
29  "%Y-%m-%dT%H:%M:%S", // 4
30  "%Y-%m-%d", // 5
31 };
32 const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
33 } // namespace
34 
35 void BM_Format_FormatTime(benchmark::State& state) {
36  const std::string fmt = kFormats[state.range(0)];
37  state.SetLabel(fmt);
38  const absl::TimeZone lax =
39  absl::time_internal::LoadTimeZone("America/Los_Angeles");
40  const absl::Time t =
41  absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
43  while (state.KeepRunning()) {
44  benchmark::DoNotOptimize(absl::FormatTime(fmt, t, lax).length());
45  }
46 }
47 BENCHMARK(BM_Format_FormatTime)->DenseRange(0, kNumFormats - 1);
48 
49 void BM_Format_ParseTime(benchmark::State& state) {
50  const std::string fmt = kFormats[state.range(0)];
51  state.SetLabel(fmt);
52  const absl::TimeZone lax =
53  absl::time_internal::LoadTimeZone("America/Los_Angeles");
54  absl::Time t = absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
56  const std::string when = absl::FormatTime(fmt, t, lax);
57  std::string err;
58  while (state.KeepRunning()) {
59  benchmark::DoNotOptimize(absl::ParseTime(fmt, when, lax, &t, &err));
60  }
61 }
62 BENCHMARK(BM_Format_ParseTime)->DenseRange(0, kNumFormats - 1);
63 
64 } // namespace
const char RFC1123_no_wday[]
Definition: time.h:1192
std::string FormatTime(const std::string &format, absl::Time t, absl::TimeZone tz)
Definition: format.cc:70
const char RFC3339_full[]
Definition: time.h:1184
bool ParseTime(const std::string &format, const std::string &input, absl::Time *time, std::string *err)
Definition: format.cc:87
const char RFC3339_sec[]
Definition: time.h:1185
Time FromCivil(CivilSecond ct, TimeZone tz)
Definition: time.h:1085
const char RFC1123_full[]
Definition: time.h:1191
TimeZone LoadTimeZone(const std::string &name)
Definition: test_util.cc:29
constexpr Duration Nanoseconds(int64_t n)
Definition: time.h:1452
std::size_t length
Definition: test_util.cc:52


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18