23 #include "benchmark/benchmark.h" 32 void BM_Difference_Days(benchmark::State& state) {
35 while (state.KeepRunning()) {
36 benchmark::DoNotOptimize(c - epoch);
39 BENCHMARK(BM_Difference_Days);
41 void BM_Step_Days(benchmark::State& state) {
44 while (state.KeepRunning()) {
45 benchmark::DoNotOptimize(++c);
48 BENCHMARK(BM_Step_Days);
50 void BM_GetWeekday(benchmark::State& state) {
52 while (state.KeepRunning()) {
56 BENCHMARK(BM_GetWeekday);
58 void BM_NextWeekday(benchmark::State& state) {
61 kStart + 0, kStart + 1, kStart + 2, kStart + 3,
62 kStart + 4, kStart + 5, kStart + 6,
65 cctz::weekday::monday, cctz::weekday::tuesday, cctz::weekday::wednesday,
66 cctz::weekday::thursday, cctz::weekday::friday, cctz::weekday::saturday,
67 cctz::weekday::sunday,
69 while (state.KeepRunningBatch(7 * 7)) {
70 for (
const auto from : kDays) {
71 for (
const auto to : kWeekdays) {
77 BENCHMARK(BM_NextWeekday);
79 void BM_PrevWeekday(benchmark::State& state) {
82 kStart + 0, kStart + 1, kStart + 2, kStart + 3,
83 kStart + 4, kStart + 5, kStart + 6,
86 cctz::weekday::monday, cctz::weekday::tuesday, cctz::weekday::wednesday,
87 cctz::weekday::thursday, cctz::weekday::friday, cctz::weekday::saturday,
88 cctz::weekday::sunday,
90 while (state.KeepRunningBatch(7 * 7)) {
91 for (
const auto from : kDays) {
92 for (
const auto to : kWeekdays) {
98 BENCHMARK(BM_PrevWeekday);
108 const char*
const kTimeZoneNames[] = {
111 "Africa/Addis_Ababa",
120 "Africa/Brazzaville",
127 "Africa/Dar_es_Salaam",
134 "Africa/Johannesburg",
156 "Africa/Ouagadougou",
168 "America/Argentina/Buenos_Aires",
169 "America/Argentina/Catamarca",
170 "America/Argentina/ComodRivadavia",
171 "America/Argentina/Cordoba",
172 "America/Argentina/Jujuy",
173 "America/Argentina/La_Rioja",
174 "America/Argentina/Mendoza",
175 "America/Argentina/Rio_Gallegos",
176 "America/Argentina/Salta",
177 "America/Argentina/San_Juan",
178 "America/Argentina/San_Luis",
179 "America/Argentina/Tucuman",
180 "America/Argentina/Ushuaia",
186 "America/Bahia_Banderas",
190 "America/Blanc-Sablon",
194 "America/Buenos_Aires",
195 "America/Cambridge_Bay",
196 "America/Campo_Grande",
204 "America/Coral_Harbour",
206 "America/Costa_Rica",
210 "America/Danmarkshavn",
212 "America/Dawson_Creek",
218 "America/El_Salvador",
220 "America/Fort_Nelson",
221 "America/Fort_Wayne",
226 "America/Grand_Turk",
228 "America/Guadeloupe",
234 "America/Hermosillo",
235 "America/Indiana/Indianapolis",
236 "America/Indiana/Knox",
237 "America/Indiana/Marengo",
238 "America/Indiana/Petersburg",
239 "America/Indiana/Tell_City",
240 "America/Indiana/Vevay",
241 "America/Indiana/Vincennes",
242 "America/Indiana/Winamac",
243 "America/Indianapolis",
249 "America/Kentucky/Louisville",
250 "America/Kentucky/Monticello",
252 "America/Kralendijk",
255 "America/Los_Angeles",
256 "America/Louisville",
257 "America/Lower_Princes",
262 "America/Martinique",
268 "America/Metlakatla",
269 "America/Mexico_City",
273 "America/Montevideo",
275 "America/Montserrat",
281 "America/North_Dakota/Beulah",
282 "America/North_Dakota/Center",
283 "America/North_Dakota/New_Salem",
286 "America/Pangnirtung",
287 "America/Paramaribo",
289 "America/Port-au-Prince",
290 "America/Port_of_Spain",
291 "America/Porto_Acre",
292 "America/Porto_Velho",
293 "America/Puerto_Rico",
294 "America/Punta_Arenas",
295 "America/Rainy_River",
296 "America/Rankin_Inlet",
300 "America/Rio_Branco",
302 "America/Santa_Isabel",
305 "America/Santo_Domingo",
307 "America/Scoresbysund",
310 "America/St_Barthelemy",
315 "America/St_Vincent",
316 "America/Swift_Current",
317 "America/Tegucigalpa",
319 "America/Thunder_Bay",
325 "America/Whitehorse",
328 "America/Yellowknife",
331 "Antarctica/DumontDUrville",
332 "Antarctica/Macquarie",
334 "Antarctica/McMurdo",
336 "Antarctica/Rothera",
337 "Antarctica/South_Pole",
341 "Arctic/Longyearbyen",
420 "Asia/Srednekolymsk",
430 "Asia/Ujung_Pandang",
439 "Asia/Yekaterinburg",
444 "Atlantic/Cape_Verde",
447 "Atlantic/Jan_Mayen",
449 "Atlantic/Reykjavik",
450 "Atlantic/South_Georgia",
451 "Atlantic/St_Helena",
454 "Australia/Adelaide",
455 "Australia/Brisbane",
456 "Australia/Broken_Hill",
457 "Australia/Canberra",
463 "Australia/Lindeman",
464 "Australia/Lord_Howe",
465 "Australia/Melbourne",
469 "Australia/Queensland",
472 "Australia/Tasmania",
473 "Australia/Victoria",
475 "Australia/Yancowinna",
486 "Canada/Newfoundland",
488 "Canada/Saskatchewan",
491 "Chile/EasterIsland",
551 "Europe/Isle_of_Man",
554 "Europe/Kaliningrad",
606 "Indian/Antananarivo",
636 "Pacific/Bougainville",
647 "Pacific/Guadalcanal",
651 "Pacific/Kiritimati",
666 "Pacific/Port_Moresby",
704 std::vector<std::string> AllTimeZoneNames() {
705 std::vector<std::string> names;
706 for (
const char*
const* namep = kTimeZoneNames; *namep !=
nullptr; ++namep) {
707 names.push_back(std::string(
"file:") + *namep);
709 assert(!names.empty());
711 std::mt19937 urbg(42);
712 std::shuffle(names.begin(), names.end(), urbg);
722 void BM_Zone_LoadUTCTimeZoneFirst(benchmark::State& state) {
725 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
726 while (state.KeepRunning()) {
730 BENCHMARK(BM_Zone_LoadUTCTimeZoneFirst);
732 void BM_Zone_LoadUTCTimeZoneLast(benchmark::State& state) {
734 for (
const auto&
name : AllTimeZoneNames()) {
737 while (state.KeepRunning()) {
741 BENCHMARK(BM_Zone_LoadUTCTimeZoneLast);
743 void BM_Zone_LoadTimeZoneFirst(benchmark::State& state) {
745 const std::string
name =
"file:America/Los_Angeles";
746 while (state.KeepRunning()) {
748 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
749 state.ResumeTiming();
753 BENCHMARK(BM_Zone_LoadTimeZoneFirst);
755 void BM_Zone_LoadTimeZoneCached(benchmark::State& state) {
757 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
758 const std::string name =
"file:America/Los_Angeles";
760 while (state.KeepRunning()) {
764 BENCHMARK(BM_Zone_LoadTimeZoneCached);
766 void BM_Zone_LoadLocalTimeZoneCached(benchmark::State& state) {
768 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
770 while (state.KeepRunning()) {
774 BENCHMARK(BM_Zone_LoadLocalTimeZoneCached);
776 void BM_Zone_LoadAllTimeZonesFirst(benchmark::State& state) {
778 const std::vector<std::string> names = AllTimeZoneNames();
779 for (
auto index = names.size(); state.KeepRunning(); ++index) {
780 if (index == names.size()) {
785 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
786 state.ResumeTiming();
791 BENCHMARK(BM_Zone_LoadAllTimeZonesFirst);
793 void BM_Zone_LoadAllTimeZonesCached(benchmark::State& state) {
795 const std::vector<std::string> names = AllTimeZoneNames();
796 for (
const auto& name : names) {
799 for (
auto index = names.size(); state.KeepRunning(); ++index) {
800 if (index == names.size()) {
806 BENCHMARK(BM_Zone_LoadAllTimeZonesCached);
808 void BM_Zone_TimeZoneEqualityImplicit(benchmark::State& state) {
810 while (state.KeepRunning()) {
811 benchmark::DoNotOptimize(tz == tz);
814 BENCHMARK(BM_Zone_TimeZoneEqualityImplicit);
816 void BM_Zone_TimeZoneEqualityExplicit(benchmark::State& state) {
818 while (state.KeepRunning()) {
819 benchmark::DoNotOptimize(tz == tz);
822 BENCHMARK(BM_Zone_TimeZoneEqualityExplicit);
824 void BM_Zone_UTCTimeZone(benchmark::State& state) {
826 while (state.KeepRunning()) {
830 BENCHMARK(BM_Zone_UTCTimeZone);
838 void BM_Time_ToCivil_CCTZ(benchmark::State& state) {
841 std::chrono::system_clock::from_time_t(1384569027);
843 std::chrono::system_clock::from_time_t(1418962578);
844 while (state.KeepRunning()) {
850 BENCHMARK(BM_Time_ToCivil_CCTZ);
852 void BM_Time_ToCivil_Libc(benchmark::State& state) {
854 time_t t = 1384569027;
855 time_t t2 = 1418962578;
857 while (state.KeepRunning()) {
860 #if defined(_WIN32) || defined(_WIN64) 861 benchmark::DoNotOptimize(localtime_s(&tm, &t));
863 benchmark::DoNotOptimize(localtime_r(&t, &tm));
867 BENCHMARK(BM_Time_ToCivil_Libc);
869 void BM_Time_ToCivilUTC_CCTZ(benchmark::State& state) {
872 std::chrono::system_clock::from_time_t(1384569027);
873 while (state.KeepRunning()) {
878 BENCHMARK(BM_Time_ToCivilUTC_CCTZ);
880 void BM_Time_ToCivilUTC_Libc(benchmark::State& state) {
881 time_t t = 1384569027;
883 while (state.KeepRunning()) {
885 #if defined(_WIN32) || defined(_WIN64) 886 benchmark::DoNotOptimize(gmtime_s(&tm, &t));
888 benchmark::DoNotOptimize(gmtime_r(&t, &tm));
892 BENCHMARK(BM_Time_ToCivilUTC_Libc);
901 void BM_Time_FromCivil_CCTZ(benchmark::State& state) {
904 while (state.KeepRunning()) {
905 if ((i++ & 1) == 0) {
906 benchmark::DoNotOptimize(
909 benchmark::DoNotOptimize(
914 BENCHMARK(BM_Time_FromCivil_CCTZ);
916 void BM_Time_FromCivil_Libc(benchmark::State& state) {
919 while (state.KeepRunning()) {
921 if ((i++ & 1) == 0) {
922 tm.tm_year = 2014 - 1900;
929 tm.tm_year = 2013 - 1900;
937 benchmark::DoNotOptimize(mktime(&tm));
940 BENCHMARK(BM_Time_FromCivil_Libc);
942 void BM_Time_FromCivilUTC_CCTZ(benchmark::State& state) {
944 while (state.KeepRunning()) {
945 benchmark::DoNotOptimize(
949 BENCHMARK(BM_Time_FromCivilUTC_CCTZ);
953 void BM_Time_FromCivilDay0_CCTZ(benchmark::State& state) {
956 while (state.KeepRunning()) {
957 if ((i++ & 1) == 0) {
958 benchmark::DoNotOptimize(
961 benchmark::DoNotOptimize(
966 BENCHMARK(BM_Time_FromCivilDay0_CCTZ);
968 void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
971 while (state.KeepRunning()) {
973 if ((i++ & 1) == 0) {
974 tm.tm_year = 2014 - 1900;
981 tm.tm_year = 2013 - 1900;
989 benchmark::DoNotOptimize(mktime(&tm));
992 BENCHMARK(BM_Time_FromCivilDay0_Libc);
994 const char*
const kFormats[] = {
1002 const int kNumFormats =
sizeof(kFormats) /
sizeof(kFormats[0]);
1004 void BM_Format_FormatTime(benchmark::State& state) {
1005 const std::string fmt = kFormats[state.range(0)];
1006 state.SetLabel(fmt);
1010 std::chrono::microseconds(1);
1011 while (state.KeepRunning()) {
1015 BENCHMARK(BM_Format_FormatTime)->DenseRange(0, kNumFormats - 1);
1017 void BM_Format_ParseTime(benchmark::State& state) {
1018 const std::string fmt = kFormats[state.range(0)];
1019 state.SetLabel(fmt);
1023 std::chrono::microseconds(1);
1025 while (state.KeepRunning()) {
1026 benchmark::DoNotOptimize(
cctz::parse(fmt, when, tz, &tp));
1029 BENCHMARK(BM_Format_ParseTime)->DenseRange(0, kNumFormats - 1);
const char RFC1123_no_wday[]
const char RFC3339_full[]
CONSTEXPR_F civil_day next_weekday(civil_day cd, weekday wd) noexcept
CONSTEXPR_F weekday get_weekday(const civil_day &cd) noexcept
std::chrono::duration< std::int_fast64_t > seconds
time_zone local_time_zone()
std::string format(const std::string &, const time_point< seconds > &, const femtoseconds &, const time_zone &)
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
CONSTEXPR_F civil_day prev_weekday(civil_day cd, weekday wd) noexcept
bool parse(const std::string &, const std::string &, const time_zone &, time_point< seconds > *, femtoseconds *, std::string *err=nullptr)
std::chrono::time_point< std::chrono::system_clock, D > time_point
const char RFC1123_full[]
time_zone utc_time_zone()
bool load_time_zone(const std::string &name, time_zone *tz)
civil_second convert(const time_point< D > &tp, const time_zone &tz)