format_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2018 The Abseil Authors.
00002 // Licensed under the Apache License, Version 2.0 (the "License");
00003 // you may not use this file except in compliance with the License.
00004 // You may obtain a copy of the License at
00005 //
00006 //      https://www.apache.org/licenses/LICENSE-2.0
00007 //
00008 // Unless required by applicable law or agreed to in writing, software
00009 // distributed under the License is distributed on an "AS IS" BASIS,
00010 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00011 // See the License for the specific language governing permissions and
00012 // limitations under the License.
00013 
00014 #include <cstddef>
00015 #include <string>
00016 
00017 #include "absl/time/internal/test_util.h"
00018 #include "absl/time/time.h"
00019 #include "benchmark/benchmark.h"
00020 
00021 namespace {
00022 
00023 namespace {
00024 const char* const kFormats[] = {
00025     absl::RFC1123_full,     // 0
00026     absl::RFC1123_no_wday,  // 1
00027     absl::RFC3339_full,     // 2
00028     absl::RFC3339_sec,      // 3
00029     "%Y-%m-%dT%H:%M:%S",    // 4
00030     "%Y-%m-%d",             // 5
00031 };
00032 const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
00033 }  // namespace
00034 
00035 void BM_Format_FormatTime(benchmark::State& state) {
00036   const std::string fmt = kFormats[state.range(0)];
00037   state.SetLabel(fmt);
00038   const absl::TimeZone lax =
00039       absl::time_internal::LoadTimeZone("America/Los_Angeles");
00040   const absl::Time t =
00041       absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
00042       absl::Nanoseconds(1);
00043   while (state.KeepRunning()) {
00044     benchmark::DoNotOptimize(absl::FormatTime(fmt, t, lax).length());
00045   }
00046 }
00047 BENCHMARK(BM_Format_FormatTime)->DenseRange(0, kNumFormats - 1);
00048 
00049 void BM_Format_ParseTime(benchmark::State& state) {
00050   const std::string fmt = kFormats[state.range(0)];
00051   state.SetLabel(fmt);
00052   const absl::TimeZone lax =
00053       absl::time_internal::LoadTimeZone("America/Los_Angeles");
00054   absl::Time t = absl::FromCivil(absl::CivilSecond(1977, 6, 28, 9, 8, 7), lax) +
00055                  absl::Nanoseconds(1);
00056   const std::string when = absl::FormatTime(fmt, t, lax);
00057   std::string err;
00058   while (state.KeepRunning()) {
00059     benchmark::DoNotOptimize(absl::ParseTime(fmt, when, lax, &t, &err));
00060   }
00061 }
00062 BENCHMARK(BM_Format_ParseTime)->DenseRange(0, kNumFormats - 1);
00063 
00064 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14