time_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 "absl/time/time.h"
00015 
00016 #if !defined(_WIN32)
00017 #include <sys/time.h>
00018 #endif  // _WIN32
00019 #include <algorithm>
00020 #include <cmath>
00021 #include <cstddef>
00022 #include <cstring>
00023 #include <ctime>
00024 #include <memory>
00025 #include <string>
00026 
00027 #include "absl/time/clock.h"
00028 #include "absl/time/internal/test_util.h"
00029 #include "benchmark/benchmark.h"
00030 
00031 namespace {
00032 
00033 //
00034 // Addition/Subtraction of a duration
00035 //
00036 
00037 void BM_Time_Arithmetic(benchmark::State& state) {
00038   const absl::Duration nano = absl::Nanoseconds(1);
00039   const absl::Duration sec = absl::Seconds(1);
00040   absl::Time t = absl::UnixEpoch();
00041   while (state.KeepRunning()) {
00042     benchmark::DoNotOptimize(t += nano);
00043     benchmark::DoNotOptimize(t -= sec);
00044   }
00045 }
00046 BENCHMARK(BM_Time_Arithmetic);
00047 
00048 //
00049 // Time difference
00050 //
00051 
00052 void BM_Time_Difference(benchmark::State& state) {
00053   absl::Time start = absl::Now();
00054   absl::Time end = start + absl::Nanoseconds(1);
00055   absl::Duration diff;
00056   while (state.KeepRunning()) {
00057     benchmark::DoNotOptimize(diff += end - start);
00058   }
00059 }
00060 BENCHMARK(BM_Time_Difference);
00061 
00062 //
00063 // ToDateTime
00064 //
00065 // In each "ToDateTime" benchmark we switch between two instants
00066 // separated by at least one transition in order to defeat any
00067 // internal caching of previous results (e.g., see local_time_hint_).
00068 //
00069 // The "UTC" variants use UTC instead of the Google/local time zone.
00070 //
00071 
00072 void BM_Time_ToDateTime_Absl(benchmark::State& state) {
00073   const absl::TimeZone tz =
00074       absl::time_internal::LoadTimeZone("America/Los_Angeles");
00075   absl::Time t = absl::FromUnixSeconds(1384569027);
00076   absl::Time t2 = absl::FromUnixSeconds(1418962578);
00077   while (state.KeepRunning()) {
00078     std::swap(t, t2);
00079     t += absl::Seconds(1);
00080     benchmark::DoNotOptimize(t.In(tz));
00081   }
00082 }
00083 BENCHMARK(BM_Time_ToDateTime_Absl);
00084 
00085 void BM_Time_ToDateTime_Libc(benchmark::State& state) {
00086   // No timezone support, so just use localtime.
00087   time_t t = 1384569027;
00088   time_t t2 = 1418962578;
00089   while (state.KeepRunning()) {
00090     std::swap(t, t2);
00091     t += 1;
00092     struct tm tm;
00093 #if !defined(_WIN32)
00094     benchmark::DoNotOptimize(localtime_r(&t, &tm));
00095 #else   // _WIN32
00096     benchmark::DoNotOptimize(localtime_s(&tm, &t));
00097 #endif  // _WIN32
00098   }
00099 }
00100 BENCHMARK(BM_Time_ToDateTime_Libc);
00101 
00102 void BM_Time_ToDateTimeUTC_Absl(benchmark::State& state) {
00103   const absl::TimeZone tz = absl::UTCTimeZone();
00104   absl::Time t = absl::FromUnixSeconds(1384569027);
00105   while (state.KeepRunning()) {
00106     t += absl::Seconds(1);
00107     benchmark::DoNotOptimize(t.In(tz));
00108   }
00109 }
00110 BENCHMARK(BM_Time_ToDateTimeUTC_Absl);
00111 
00112 void BM_Time_ToDateTimeUTC_Libc(benchmark::State& state) {
00113   time_t t = 1384569027;
00114   while (state.KeepRunning()) {
00115     t += 1;
00116     struct tm tm;
00117 #if !defined(_WIN32)
00118     benchmark::DoNotOptimize(gmtime_r(&t, &tm));
00119 #else   // _WIN32
00120     benchmark::DoNotOptimize(gmtime_s(&tm, &t));
00121 #endif  // _WIN32
00122   }
00123 }
00124 BENCHMARK(BM_Time_ToDateTimeUTC_Libc);
00125 
00126 //
00127 // FromUnixMicros
00128 //
00129 
00130 void BM_Time_FromUnixMicros(benchmark::State& state) {
00131   int i = 0;
00132   while (state.KeepRunning()) {
00133     benchmark::DoNotOptimize(absl::FromUnixMicros(i));
00134     ++i;
00135   }
00136 }
00137 BENCHMARK(BM_Time_FromUnixMicros);
00138 
00139 void BM_Time_ToUnixNanos(benchmark::State& state) {
00140   const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
00141   while (state.KeepRunning()) {
00142     benchmark::DoNotOptimize(ToUnixNanos(t));
00143   }
00144 }
00145 BENCHMARK(BM_Time_ToUnixNanos);
00146 
00147 void BM_Time_ToUnixMicros(benchmark::State& state) {
00148   const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
00149   while (state.KeepRunning()) {
00150     benchmark::DoNotOptimize(ToUnixMicros(t));
00151   }
00152 }
00153 BENCHMARK(BM_Time_ToUnixMicros);
00154 
00155 void BM_Time_ToUnixMillis(benchmark::State& state) {
00156   const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
00157   while (state.KeepRunning()) {
00158     benchmark::DoNotOptimize(ToUnixMillis(t));
00159   }
00160 }
00161 BENCHMARK(BM_Time_ToUnixMillis);
00162 
00163 void BM_Time_ToUnixSeconds(benchmark::State& state) {
00164   const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
00165   while (state.KeepRunning()) {
00166     benchmark::DoNotOptimize(absl::ToUnixSeconds(t));
00167   }
00168 }
00169 BENCHMARK(BM_Time_ToUnixSeconds);
00170 
00171 //
00172 // FromCivil
00173 //
00174 // In each "FromCivil" benchmark we switch between two YMDhms values
00175 // separated by at least one transition in order to defeat any internal
00176 // caching of previous results (e.g., see time_local_hint_).
00177 //
00178 // The "UTC" variants use UTC instead of the Google/local time zone.
00179 // The "Day0" variants require normalization of the day of month.
00180 //
00181 
00182 void BM_Time_FromCivil_Absl(benchmark::State& state) {
00183   const absl::TimeZone tz =
00184       absl::time_internal::LoadTimeZone("America/Los_Angeles");
00185   int i = 0;
00186   while (state.KeepRunning()) {
00187     if ((i & 1) == 0) {
00188       absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
00189     } else {
00190       absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz);
00191     }
00192     ++i;
00193   }
00194 }
00195 BENCHMARK(BM_Time_FromCivil_Absl);
00196 
00197 void BM_Time_FromCivil_Libc(benchmark::State& state) {
00198   // No timezone support, so just use localtime.
00199   int i = 0;
00200   while (state.KeepRunning()) {
00201     struct tm tm;
00202     if ((i & 1) == 0) {
00203       tm.tm_year = 2014 - 1900;
00204       tm.tm_mon = 12 - 1;
00205       tm.tm_mday = 18;
00206       tm.tm_hour = 20;
00207       tm.tm_min = 16;
00208       tm.tm_sec = 18;
00209     } else {
00210       tm.tm_year = 2013 - 1900;
00211       tm.tm_mon = 11 - 1;
00212       tm.tm_mday = 15;
00213       tm.tm_hour = 18;
00214       tm.tm_min = 30;
00215       tm.tm_sec = 27;
00216     }
00217     tm.tm_isdst = -1;
00218     mktime(&tm);
00219     ++i;
00220   }
00221 }
00222 BENCHMARK(BM_Time_FromCivil_Libc);
00223 
00224 void BM_Time_FromCivilUTC_Absl(benchmark::State& state) {
00225   const absl::TimeZone tz = absl::UTCTimeZone();
00226   while (state.KeepRunning()) {
00227     absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
00228   }
00229 }
00230 BENCHMARK(BM_Time_FromCivilUTC_Absl);
00231 
00232 void BM_Time_FromCivilDay0_Absl(benchmark::State& state) {
00233   const absl::TimeZone tz =
00234       absl::time_internal::LoadTimeZone("America/Los_Angeles");
00235   int i = 0;
00236   while (state.KeepRunning()) {
00237     if ((i & 1) == 0) {
00238       absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz);
00239     } else {
00240       absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz);
00241     }
00242     ++i;
00243   }
00244 }
00245 BENCHMARK(BM_Time_FromCivilDay0_Absl);
00246 
00247 void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
00248   // No timezone support, so just use localtime.
00249   int i = 0;
00250   while (state.KeepRunning()) {
00251     struct tm tm;
00252     if ((i & 1) == 0) {
00253       tm.tm_year = 2014 - 1900;
00254       tm.tm_mon = 12 - 1;
00255       tm.tm_mday = 0;
00256       tm.tm_hour = 20;
00257       tm.tm_min = 16;
00258       tm.tm_sec = 18;
00259     } else {
00260       tm.tm_year = 2013 - 1900;
00261       tm.tm_mon = 11 - 1;
00262       tm.tm_mday = 0;
00263       tm.tm_hour = 18;
00264       tm.tm_min = 30;
00265       tm.tm_sec = 27;
00266     }
00267     tm.tm_isdst = -1;
00268     mktime(&tm);
00269     ++i;
00270   }
00271 }
00272 BENCHMARK(BM_Time_FromCivilDay0_Libc);
00273 
00274 //
00275 // To/FromTimespec
00276 //
00277 
00278 void BM_Time_ToTimespec(benchmark::State& state) {
00279   absl::Time now = absl::Now();
00280   while (state.KeepRunning()) {
00281     benchmark::DoNotOptimize(absl::ToTimespec(now));
00282   }
00283 }
00284 BENCHMARK(BM_Time_ToTimespec);
00285 
00286 void BM_Time_FromTimespec(benchmark::State& state) {
00287   timespec ts = absl::ToTimespec(absl::Now());
00288   while (state.KeepRunning()) {
00289     if (++ts.tv_nsec == 1000 * 1000 * 1000) {
00290       ++ts.tv_sec;
00291       ts.tv_nsec = 0;
00292     }
00293     benchmark::DoNotOptimize(absl::TimeFromTimespec(ts));
00294   }
00295 }
00296 BENCHMARK(BM_Time_FromTimespec);
00297 
00298 //
00299 // Comparison with InfiniteFuture/Past
00300 //
00301 
00302 void BM_Time_InfiniteFuture(benchmark::State& state) {
00303   while (state.KeepRunning()) {
00304     benchmark::DoNotOptimize(absl::InfiniteFuture());
00305   }
00306 }
00307 BENCHMARK(BM_Time_InfiniteFuture);
00308 
00309 void BM_Time_InfinitePast(benchmark::State& state) {
00310   while (state.KeepRunning()) {
00311     benchmark::DoNotOptimize(absl::InfinitePast());
00312   }
00313 }
00314 BENCHMARK(BM_Time_InfinitePast);
00315 
00316 }  // namespace


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