Files
abseil-cpp/absl/time/time_benchmark.cc
Abseil Team aa844899c9 Creation of LTS branch "lts_2019_08_08"
- 9ee91d3e43 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 8efba58a3b Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - b49b8d16b6 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 67222ffc4c Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - c5c4db4f51 Export of internal Abseil changes by Abseil Team <absl-team@google.com>
  - 14550beb3b Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 52e88ee56b Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 36d37ab992 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - ad1485c898 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - f3840bc5e3 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 278b26058c Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - c6c3c1b498 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 44efe96dfc Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 3c98fcc046 Merge pull request #340 from jtsylve/macos_cxx17_fix by Matt Calabrese <38107210+mattcalabrese-google@users.noreply.github.com>
  - 74d91756c1 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - e6b050212c Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - c964fcffac Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 72e09a54d9 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - d65e19dfcd Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 5162fc83d2 Merge pull request #336 from shields/patch-2 by Shaindel Schwartz <31392632+shaindelschwartz@users.noreply.github.com>
  - 0389f7bf58 Merge pull request #335 from shields/patch-1 by Shaindel Schwartz <31392632+shaindelschwartz@users.noreply.github.com>
  - e9324d926a Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 43ef2148c0 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - a13d3df2b3 Merge pull request #323 from gosnik/master by Gennadiy Rozental <rogeeff@google.com>
  - 310a11865c Merge pull request #324 from RasPat1/patch-1 by Gennadiy Rozental <rogeeff@google.com>
  - 8f11724067 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - b1dd425423 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 361cb8a9db Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 0238ab0a83 Merge pull request #321 from christoph-cullmann/c4245_fix... by Xiaoyi Zhang <zhangxy988@gmail.com>
  - 61c9bf3e3e Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - bc9101f998 Merge pull request #320 from christoph-cullmann/master by Xiaoyi Zhang <zhangxy988@gmail.com>
  - 2f76a9bf50 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 4adaf54909 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 27c30ec671 Avoid undefined behavior when nullptr is passed to memcpy... by Roman Gershman <romange@gmail.com>
  - ce65f5ac3c Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - a18fc7461e Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 8a394b19c1 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - daf381e853 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - fa00c32107 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 436ba6c4a0 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 0cbdc774b9 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 27c2f6e2f3 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - aa468ad755 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - cd86d0d20a Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 33841c5c96 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - ca3f87560a Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - d902eb869b Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - a02f62f456 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 0b545b4601 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - dbae8764fb Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 044da8a29c Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 6cc6ac44e0 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 666fc1266b Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 93dfcf74cb Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 2c8421e1c6 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 5b65c4af51 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - eab2078b53 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 253eb74164 [CMake] Set correct flags for clang-cl (#278) by Loo Rong Jie <loorongjie@gmail.com>
  - e75672f6af Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - bf29470384 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 6fd827124f Merge pull request #280 from chiumichael/master by Derek Mauro <761129+derekmauro@users.noreply.github.com>
  - 7c7754fb3e Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 256be56344 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 88a152ae74 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - c1cecb25a9 Implement Span::first and Span::last from C++20 (#274) by Girts <girtsf@users.noreply.github.com>
  - 38b704384c Changed HTTP URLs to HTTPS where possible (#270) by nik7273 <nik8470@gmail.com>
  - febc5ee6a9 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 9fdf5e5b80 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 419f3184f8 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - b312c3cb53 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 308ce31528 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 93d155bc44 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 426eaa4aa4 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 2901ec32a9 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - d78310fe5a Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - a4cb1c8ba6 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 540e2537b9 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 89ea0c5ff3 Merge pull request #255 from uilianries/hotfix/conan by ahedberg <ahedberg@google.com>
  - 5e0dcf72c6 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 0dffca4e36 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 6b4201f9ef Fix GCC8 warnings by Boris Staletic <boris.staletic@gmail.com>
  - 0b1e6d417b Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - efccc50260 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 5e6a78131f Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 5eea0f713c Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 66f9becbb9 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 018b4db1d7 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 9449ae9439 Merge pull request #243 from ThomsonTan/FixIntrinsic by Alex Strelnikov <strel@google.com>
  - b16aeb6756 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 7ffbe09f3d Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 01b471d9f3 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 7bd8f36c74 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 968a34ffda Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 3e2e9b5557 Merge pull request #231 from uilianries/feature/conan by Mark Barolak <mbxx@users.noreply.github.com>
  - 111ca7060a Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 389ec3f906 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 8fbcdb9095 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 455dc17ba1 Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - f197d7c72a Export of internal Abseil changes. by Abseil Team <absl-team@google.com>
  - 284378a71b Export of internal Abseil changes. by Abseil Team <absl-team@google.com>

GitOrigin-RevId: 9ee91d3e43
Change-Id: Ia06e548bc106cc9d136f6c65714be6645317aced
2019-08-08 14:19:45 -04:00

317 lines
8.1 KiB
C++

// Copyright 2018 The Abseil Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/time/time.h"
#if !defined(_WIN32)
#include <sys/time.h>
#endif // _WIN32
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <cstring>
#include <ctime>
#include <memory>
#include <string>
#include "absl/time/clock.h"
#include "absl/time/internal/test_util.h"
#include "benchmark/benchmark.h"
namespace {
//
// Addition/Subtraction of a duration
//
void BM_Time_Arithmetic(benchmark::State& state) {
const absl::Duration nano = absl::Nanoseconds(1);
const absl::Duration sec = absl::Seconds(1);
absl::Time t = absl::UnixEpoch();
while (state.KeepRunning()) {
benchmark::DoNotOptimize(t += nano);
benchmark::DoNotOptimize(t -= sec);
}
}
BENCHMARK(BM_Time_Arithmetic);
//
// Time difference
//
void BM_Time_Difference(benchmark::State& state) {
absl::Time start = absl::Now();
absl::Time end = start + absl::Nanoseconds(1);
absl::Duration diff;
while (state.KeepRunning()) {
benchmark::DoNotOptimize(diff += end - start);
}
}
BENCHMARK(BM_Time_Difference);
//
// ToDateTime
//
// In each "ToDateTime" benchmark we switch between two instants
// separated by at least one transition in order to defeat any
// internal caching of previous results (e.g., see local_time_hint_).
//
// The "UTC" variants use UTC instead of the Google/local time zone.
//
void BM_Time_ToDateTime_Absl(benchmark::State& state) {
const absl::TimeZone tz =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
absl::Time t = absl::FromUnixSeconds(1384569027);
absl::Time t2 = absl::FromUnixSeconds(1418962578);
while (state.KeepRunning()) {
std::swap(t, t2);
t += absl::Seconds(1);
benchmark::DoNotOptimize(t.In(tz));
}
}
BENCHMARK(BM_Time_ToDateTime_Absl);
void BM_Time_ToDateTime_Libc(benchmark::State& state) {
// No timezone support, so just use localtime.
time_t t = 1384569027;
time_t t2 = 1418962578;
while (state.KeepRunning()) {
std::swap(t, t2);
t += 1;
struct tm tm;
#if !defined(_WIN32)
benchmark::DoNotOptimize(localtime_r(&t, &tm));
#else // _WIN32
benchmark::DoNotOptimize(localtime_s(&tm, &t));
#endif // _WIN32
}
}
BENCHMARK(BM_Time_ToDateTime_Libc);
void BM_Time_ToDateTimeUTC_Absl(benchmark::State& state) {
const absl::TimeZone tz = absl::UTCTimeZone();
absl::Time t = absl::FromUnixSeconds(1384569027);
while (state.KeepRunning()) {
t += absl::Seconds(1);
benchmark::DoNotOptimize(t.In(tz));
}
}
BENCHMARK(BM_Time_ToDateTimeUTC_Absl);
void BM_Time_ToDateTimeUTC_Libc(benchmark::State& state) {
time_t t = 1384569027;
while (state.KeepRunning()) {
t += 1;
struct tm tm;
#if !defined(_WIN32)
benchmark::DoNotOptimize(gmtime_r(&t, &tm));
#else // _WIN32
benchmark::DoNotOptimize(gmtime_s(&tm, &t));
#endif // _WIN32
}
}
BENCHMARK(BM_Time_ToDateTimeUTC_Libc);
//
// FromUnixMicros
//
void BM_Time_FromUnixMicros(benchmark::State& state) {
int i = 0;
while (state.KeepRunning()) {
benchmark::DoNotOptimize(absl::FromUnixMicros(i));
++i;
}
}
BENCHMARK(BM_Time_FromUnixMicros);
void BM_Time_ToUnixNanos(benchmark::State& state) {
const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
while (state.KeepRunning()) {
benchmark::DoNotOptimize(ToUnixNanos(t));
}
}
BENCHMARK(BM_Time_ToUnixNanos);
void BM_Time_ToUnixMicros(benchmark::State& state) {
const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
while (state.KeepRunning()) {
benchmark::DoNotOptimize(ToUnixMicros(t));
}
}
BENCHMARK(BM_Time_ToUnixMicros);
void BM_Time_ToUnixMillis(benchmark::State& state) {
const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
while (state.KeepRunning()) {
benchmark::DoNotOptimize(ToUnixMillis(t));
}
}
BENCHMARK(BM_Time_ToUnixMillis);
void BM_Time_ToUnixSeconds(benchmark::State& state) {
const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
while (state.KeepRunning()) {
benchmark::DoNotOptimize(absl::ToUnixSeconds(t));
}
}
BENCHMARK(BM_Time_ToUnixSeconds);
//
// FromCivil
//
// In each "FromCivil" benchmark we switch between two YMDhms values
// separated by at least one transition in order to defeat any internal
// caching of previous results (e.g., see time_local_hint_).
//
// The "UTC" variants use UTC instead of the Google/local time zone.
// The "Day0" variants require normalization of the day of month.
//
void BM_Time_FromCivil_Absl(benchmark::State& state) {
const absl::TimeZone tz =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
int i = 0;
while (state.KeepRunning()) {
if ((i & 1) == 0) {
absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
} else {
absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz);
}
++i;
}
}
BENCHMARK(BM_Time_FromCivil_Absl);
void BM_Time_FromCivil_Libc(benchmark::State& state) {
// No timezone support, so just use localtime.
int i = 0;
while (state.KeepRunning()) {
struct tm tm;
if ((i & 1) == 0) {
tm.tm_year = 2014 - 1900;
tm.tm_mon = 12 - 1;
tm.tm_mday = 18;
tm.tm_hour = 20;
tm.tm_min = 16;
tm.tm_sec = 18;
} else {
tm.tm_year = 2013 - 1900;
tm.tm_mon = 11 - 1;
tm.tm_mday = 15;
tm.tm_hour = 18;
tm.tm_min = 30;
tm.tm_sec = 27;
}
tm.tm_isdst = -1;
mktime(&tm);
++i;
}
}
BENCHMARK(BM_Time_FromCivil_Libc);
void BM_Time_FromCivilUTC_Absl(benchmark::State& state) {
const absl::TimeZone tz = absl::UTCTimeZone();
while (state.KeepRunning()) {
absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
}
}
BENCHMARK(BM_Time_FromCivilUTC_Absl);
void BM_Time_FromCivilDay0_Absl(benchmark::State& state) {
const absl::TimeZone tz =
absl::time_internal::LoadTimeZone("America/Los_Angeles");
int i = 0;
while (state.KeepRunning()) {
if ((i & 1) == 0) {
absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz);
} else {
absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz);
}
++i;
}
}
BENCHMARK(BM_Time_FromCivilDay0_Absl);
void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
// No timezone support, so just use localtime.
int i = 0;
while (state.KeepRunning()) {
struct tm tm;
if ((i & 1) == 0) {
tm.tm_year = 2014 - 1900;
tm.tm_mon = 12 - 1;
tm.tm_mday = 0;
tm.tm_hour = 20;
tm.tm_min = 16;
tm.tm_sec = 18;
} else {
tm.tm_year = 2013 - 1900;
tm.tm_mon = 11 - 1;
tm.tm_mday = 0;
tm.tm_hour = 18;
tm.tm_min = 30;
tm.tm_sec = 27;
}
tm.tm_isdst = -1;
mktime(&tm);
++i;
}
}
BENCHMARK(BM_Time_FromCivilDay0_Libc);
//
// To/FromTimespec
//
void BM_Time_ToTimespec(benchmark::State& state) {
absl::Time now = absl::Now();
while (state.KeepRunning()) {
benchmark::DoNotOptimize(absl::ToTimespec(now));
}
}
BENCHMARK(BM_Time_ToTimespec);
void BM_Time_FromTimespec(benchmark::State& state) {
timespec ts = absl::ToTimespec(absl::Now());
while (state.KeepRunning()) {
if (++ts.tv_nsec == 1000 * 1000 * 1000) {
++ts.tv_sec;
ts.tv_nsec = 0;
}
benchmark::DoNotOptimize(absl::TimeFromTimespec(ts));
}
}
BENCHMARK(BM_Time_FromTimespec);
//
// Comparison with InfiniteFuture/Past
//
void BM_Time_InfiniteFuture(benchmark::State& state) {
while (state.KeepRunning()) {
benchmark::DoNotOptimize(absl::InfiniteFuture());
}
}
BENCHMARK(BM_Time_InfiniteFuture);
void BM_Time_InfinitePast(benchmark::State& state) {
while (state.KeepRunning()) {
benchmark::DoNotOptimize(absl::InfinitePast());
}
}
BENCHMARK(BM_Time_InfinitePast);
} // namespace