civil_time_benchmark.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "absl/time/civil_time.h"
  15. #include <numeric>
  16. #include <vector>
  17. #include "absl/hash/hash.h"
  18. #include "benchmark/benchmark.h"
  19. namespace {
  20. // Run on (12 X 3492 MHz CPUs); 2018-11-05T13:44:29.814239103-08:00
  21. // CPU: Intel Haswell with HyperThreading (6 cores) dL1:32KB dL2:256KB dL3:15MB
  22. // Benchmark Time(ns) CPU(ns) Iterations
  23. // ----------------------------------------------------------------
  24. // BM_Difference_Days 14.5 14.5 48531105
  25. // BM_Step_Days 12.6 12.6 54876006
  26. // BM_Format 587 587 1000000
  27. // BM_Parse 692 692 1000000
  28. // BM_RoundTripFormatParse 1309 1309 532075
  29. // BM_CivilYearAbslHash 0.710 0.710 976400000
  30. // BM_CivilMonthAbslHash 1.13 1.13 619500000
  31. // BM_CivilDayAbslHash 1.70 1.70 426000000
  32. // BM_CivilHourAbslHash 2.45 2.45 287600000
  33. // BM_CivilMinuteAbslHash 3.21 3.21 226200000
  34. // BM_CivilSecondAbslHash 4.10 4.10 171800000
  35. void BM_Difference_Days(benchmark::State& state) {
  36. const absl::CivilDay c(2014, 8, 22);
  37. const absl::CivilDay epoch(1970, 1, 1);
  38. while (state.KeepRunning()) {
  39. const absl::civil_diff_t n = c - epoch;
  40. benchmark::DoNotOptimize(n);
  41. }
  42. }
  43. BENCHMARK(BM_Difference_Days);
  44. void BM_Step_Days(benchmark::State& state) {
  45. const absl::CivilDay kStart(2014, 8, 22);
  46. absl::CivilDay c = kStart;
  47. while (state.KeepRunning()) {
  48. benchmark::DoNotOptimize(++c);
  49. }
  50. }
  51. BENCHMARK(BM_Step_Days);
  52. void BM_Format(benchmark::State& state) {
  53. const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
  54. while (state.KeepRunning()) {
  55. const std::string s = absl::FormatCivilTime(c);
  56. benchmark::DoNotOptimize(s);
  57. }
  58. }
  59. BENCHMARK(BM_Format);
  60. void BM_Parse(benchmark::State& state) {
  61. const std::string f = "2014-01-02T03:04:05";
  62. absl::CivilSecond c;
  63. while (state.KeepRunning()) {
  64. const bool b = absl::ParseCivilTime(f, &c);
  65. benchmark::DoNotOptimize(b);
  66. }
  67. }
  68. BENCHMARK(BM_Parse);
  69. void BM_RoundTripFormatParse(benchmark::State& state) {
  70. const absl::CivilSecond c(2014, 1, 2, 3, 4, 5);
  71. absl::CivilSecond out;
  72. while (state.KeepRunning()) {
  73. const bool b = absl::ParseCivilTime(absl::FormatCivilTime(c), &out);
  74. benchmark::DoNotOptimize(b);
  75. }
  76. }
  77. BENCHMARK(BM_RoundTripFormatParse);
  78. template <typename T>
  79. void BM_CivilTimeAbslHash(benchmark::State& state) {
  80. const int kSize = 100000;
  81. std::vector<T> civil_times(kSize);
  82. std::iota(civil_times.begin(), civil_times.end(), T(2018));
  83. absl::Hash<T> absl_hasher;
  84. while (state.KeepRunningBatch(kSize)) {
  85. for (const T civil_time : civil_times) {
  86. benchmark::DoNotOptimize(absl_hasher(civil_time));
  87. }
  88. }
  89. }
  90. void BM_CivilYearAbslHash(benchmark::State& state) {
  91. BM_CivilTimeAbslHash<absl::CivilYear>(state);
  92. }
  93. void BM_CivilMonthAbslHash(benchmark::State& state) {
  94. BM_CivilTimeAbslHash<absl::CivilMonth>(state);
  95. }
  96. void BM_CivilDayAbslHash(benchmark::State& state) {
  97. BM_CivilTimeAbslHash<absl::CivilDay>(state);
  98. }
  99. void BM_CivilHourAbslHash(benchmark::State& state) {
  100. BM_CivilTimeAbslHash<absl::CivilHour>(state);
  101. }
  102. void BM_CivilMinuteAbslHash(benchmark::State& state) {
  103. BM_CivilTimeAbslHash<absl::CivilMinute>(state);
  104. }
  105. void BM_CivilSecondAbslHash(benchmark::State& state) {
  106. BM_CivilTimeAbslHash<absl::CivilSecond>(state);
  107. }
  108. BENCHMARK(BM_CivilYearAbslHash);
  109. BENCHMARK(BM_CivilMonthAbslHash);
  110. BENCHMARK(BM_CivilDayAbslHash);
  111. BENCHMARK(BM_CivilHourAbslHash);
  112. BENCHMARK(BM_CivilMinuteAbslHash);
  113. BENCHMARK(BM_CivilSecondAbslHash);
  114. } // namespace