1 // Copyright 2016 Google Inc. All Rights Reserved.
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
15 #include <algorithm>
16 #include <cassert>
17 #include <chrono>
18 #include <ctime>
19 #include <random>
20 #include <string>
21 #include <vector>
22
23 #include "benchmark/benchmark.h"
24 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
25 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
26 #include "time_zone_impl.h"
27
28 namespace {
29
30 namespace cctz = absl::time_internal::cctz;
31
BM_Difference_Days(benchmark::State & state)32 void BM_Difference_Days(benchmark::State& state) {
33 const cctz::civil_day c(2014, 8, 22);
34 const cctz::civil_day epoch(1970, 1, 1);
35 while (state.KeepRunning()) {
36 benchmark::DoNotOptimize(c - epoch);
37 }
38 }
39 BENCHMARK(BM_Difference_Days);
40
BM_Step_Days(benchmark::State & state)41 void BM_Step_Days(benchmark::State& state) {
42 const cctz::civil_day kStart(2014, 8, 22);
43 cctz::civil_day c = kStart;
44 while (state.KeepRunning()) {
45 benchmark::DoNotOptimize(++c);
46 }
47 }
48 BENCHMARK(BM_Step_Days);
49
BM_GetWeekday(benchmark::State & state)50 void BM_GetWeekday(benchmark::State& state) {
51 const cctz::civil_day c(2014, 8, 22);
52 while (state.KeepRunning()) {
53 benchmark::DoNotOptimize(cctz::get_weekday(c));
54 }
55 }
56 BENCHMARK(BM_GetWeekday);
57
BM_NextWeekday(benchmark::State & state)58 void BM_NextWeekday(benchmark::State& state) {
59 const cctz::civil_day kStart(2014, 8, 22);
60 const cctz::civil_day kDays[7] = {
61 kStart + 0, kStart + 1, kStart + 2, kStart + 3,
62 kStart + 4, kStart + 5, kStart + 6,
63 };
64 const cctz::weekday kWeekdays[7] = {
65 cctz::weekday::monday, cctz::weekday::tuesday, cctz::weekday::wednesday,
66 cctz::weekday::thursday, cctz::weekday::friday, cctz::weekday::saturday,
67 cctz::weekday::sunday,
68 };
69 while (state.KeepRunningBatch(7 * 7)) {
70 for (const auto from : kDays) {
71 for (const auto to : kWeekdays) {
72 benchmark::DoNotOptimize(cctz::next_weekday(from, to));
73 }
74 }
75 }
76 }
77 BENCHMARK(BM_NextWeekday);
78
BM_PrevWeekday(benchmark::State & state)79 void BM_PrevWeekday(benchmark::State& state) {
80 const cctz::civil_day kStart(2014, 8, 22);
81 const cctz::civil_day kDays[7] = {
82 kStart + 0, kStart + 1, kStart + 2, kStart + 3,
83 kStart + 4, kStart + 5, kStart + 6,
84 };
85 const cctz::weekday kWeekdays[7] = {
86 cctz::weekday::monday, cctz::weekday::tuesday, cctz::weekday::wednesday,
87 cctz::weekday::thursday, cctz::weekday::friday, cctz::weekday::saturday,
88 cctz::weekday::sunday,
89 };
90 while (state.KeepRunningBatch(7 * 7)) {
91 for (const auto from : kDays) {
92 for (const auto to : kWeekdays) {
93 benchmark::DoNotOptimize(cctz::prev_weekday(from, to));
94 }
95 }
96 }
97 }
98 BENCHMARK(BM_PrevWeekday);
99
100 const char RFC3339_full[] = "%Y-%m-%d%ET%H:%M:%E*S%Ez";
101 const char RFC3339_sec[] = "%Y-%m-%d%ET%H:%M:%S%Ez";
102
103 const char RFC1123_full[] = "%a, %d %b %Y %H:%M:%S %z";
104 const char RFC1123_no_wday[] = "%d %b %Y %H:%M:%S %z";
105
106 // A list of known time-zone names.
107 // TODO: Refactor with src/time_zone_lookup_test.cc.
108 const char* const kTimeZoneNames[] = {"Africa/Abidjan",
109 "Africa/Accra",
110 "Africa/Addis_Ababa",
111 "Africa/Algiers",
112 "Africa/Asmara",
113 "Africa/Asmera",
114 "Africa/Bamako",
115 "Africa/Bangui",
116 "Africa/Banjul",
117 "Africa/Bissau",
118 "Africa/Blantyre",
119 "Africa/Brazzaville",
120 "Africa/Bujumbura",
121 "Africa/Cairo",
122 "Africa/Casablanca",
123 "Africa/Ceuta",
124 "Africa/Conakry",
125 "Africa/Dakar",
126 "Africa/Dar_es_Salaam",
127 "Africa/Djibouti",
128 "Africa/Douala",
129 "Africa/El_Aaiun",
130 "Africa/Freetown",
131 "Africa/Gaborone",
132 "Africa/Harare",
133 "Africa/Johannesburg",
134 "Africa/Juba",
135 "Africa/Kampala",
136 "Africa/Khartoum",
137 "Africa/Kigali",
138 "Africa/Kinshasa",
139 "Africa/Lagos",
140 "Africa/Libreville",
141 "Africa/Lome",
142 "Africa/Luanda",
143 "Africa/Lubumbashi",
144 "Africa/Lusaka",
145 "Africa/Malabo",
146 "Africa/Maputo",
147 "Africa/Maseru",
148 "Africa/Mbabane",
149 "Africa/Mogadishu",
150 "Africa/Monrovia",
151 "Africa/Nairobi",
152 "Africa/Ndjamena",
153 "Africa/Niamey",
154 "Africa/Nouakchott",
155 "Africa/Ouagadougou",
156 "Africa/Porto-Novo",
157 "Africa/Sao_Tome",
158 "Africa/Timbuktu",
159 "Africa/Tripoli",
160 "Africa/Tunis",
161 "Africa/Windhoek",
162 "America/Adak",
163 "America/Anchorage",
164 "America/Anguilla",
165 "America/Antigua",
166 "America/Araguaina",
167 "America/Argentina/Buenos_Aires",
168 "America/Argentina/Catamarca",
169 "America/Argentina/ComodRivadavia",
170 "America/Argentina/Cordoba",
171 "America/Argentina/Jujuy",
172 "America/Argentina/La_Rioja",
173 "America/Argentina/Mendoza",
174 "America/Argentina/Rio_Gallegos",
175 "America/Argentina/Salta",
176 "America/Argentina/San_Juan",
177 "America/Argentina/San_Luis",
178 "America/Argentina/Tucuman",
179 "America/Argentina/Ushuaia",
180 "America/Aruba",
181 "America/Asuncion",
182 "America/Atikokan",
183 "America/Atka",
184 "America/Bahia",
185 "America/Bahia_Banderas",
186 "America/Barbados",
187 "America/Belem",
188 "America/Belize",
189 "America/Blanc-Sablon",
190 "America/Boa_Vista",
191 "America/Bogota",
192 "America/Boise",
193 "America/Buenos_Aires",
194 "America/Cambridge_Bay",
195 "America/Campo_Grande",
196 "America/Cancun",
197 "America/Caracas",
198 "America/Catamarca",
199 "America/Cayenne",
200 "America/Cayman",
201 "America/Chicago",
202 "America/Chihuahua",
203 "America/Coral_Harbour",
204 "America/Cordoba",
205 "America/Costa_Rica",
206 "America/Creston",
207 "America/Cuiaba",
208 "America/Curacao",
209 "America/Danmarkshavn",
210 "America/Dawson",
211 "America/Dawson_Creek",
212 "America/Denver",
213 "America/Detroit",
214 "America/Dominica",
215 "America/Edmonton",
216 "America/Eirunepe",
217 "America/El_Salvador",
218 "America/Ensenada",
219 "America/Fort_Nelson",
220 "America/Fort_Wayne",
221 "America/Fortaleza",
222 "America/Glace_Bay",
223 "America/Godthab",
224 "America/Goose_Bay",
225 "America/Grand_Turk",
226 "America/Grenada",
227 "America/Guadeloupe",
228 "America/Guatemala",
229 "America/Guayaquil",
230 "America/Guyana",
231 "America/Halifax",
232 "America/Havana",
233 "America/Hermosillo",
234 "America/Indiana/Indianapolis",
235 "America/Indiana/Knox",
236 "America/Indiana/Marengo",
237 "America/Indiana/Petersburg",
238 "America/Indiana/Tell_City",
239 "America/Indiana/Vevay",
240 "America/Indiana/Vincennes",
241 "America/Indiana/Winamac",
242 "America/Indianapolis",
243 "America/Inuvik",
244 "America/Iqaluit",
245 "America/Jamaica",
246 "America/Jujuy",
247 "America/Juneau",
248 "America/Kentucky/Louisville",
249 "America/Kentucky/Monticello",
250 "America/Knox_IN",
251 "America/Kralendijk",
252 "America/La_Paz",
253 "America/Lima",
254 "America/Los_Angeles",
255 "America/Louisville",
256 "America/Lower_Princes",
257 "America/Maceio",
258 "America/Managua",
259 "America/Manaus",
260 "America/Marigot",
261 "America/Martinique",
262 "America/Matamoros",
263 "America/Mazatlan",
264 "America/Mendoza",
265 "America/Menominee",
266 "America/Merida",
267 "America/Metlakatla",
268 "America/Mexico_City",
269 "America/Miquelon",
270 "America/Moncton",
271 "America/Monterrey",
272 "America/Montevideo",
273 "America/Montreal",
274 "America/Montserrat",
275 "America/Nassau",
276 "America/New_York",
277 "America/Nipigon",
278 "America/Nome",
279 "America/Noronha",
280 "America/North_Dakota/Beulah",
281 "America/North_Dakota/Center",
282 "America/North_Dakota/New_Salem",
283 "America/Nuuk",
284 "America/Ojinaga",
285 "America/Panama",
286 "America/Pangnirtung",
287 "America/Paramaribo",
288 "America/Phoenix",
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",
297 "America/Recife",
298 "America/Regina",
299 "America/Resolute",
300 "America/Rio_Branco",
301 "America/Rosario",
302 "America/Santa_Isabel",
303 "America/Santarem",
304 "America/Santiago",
305 "America/Santo_Domingo",
306 "America/Sao_Paulo",
307 "America/Scoresbysund",
308 "America/Shiprock",
309 "America/Sitka",
310 "America/St_Barthelemy",
311 "America/St_Johns",
312 "America/St_Kitts",
313 "America/St_Lucia",
314 "America/St_Thomas",
315 "America/St_Vincent",
316 "America/Swift_Current",
317 "America/Tegucigalpa",
318 "America/Thule",
319 "America/Thunder_Bay",
320 "America/Tijuana",
321 "America/Toronto",
322 "America/Tortola",
323 "America/Vancouver",
324 "America/Virgin",
325 "America/Whitehorse",
326 "America/Winnipeg",
327 "America/Yakutat",
328 "America/Yellowknife",
329 "Antarctica/Casey",
330 "Antarctica/Davis",
331 "Antarctica/DumontDUrville",
332 "Antarctica/Macquarie",
333 "Antarctica/Mawson",
334 "Antarctica/McMurdo",
335 "Antarctica/Palmer",
336 "Antarctica/Rothera",
337 "Antarctica/South_Pole",
338 "Antarctica/Syowa",
339 "Antarctica/Troll",
340 "Antarctica/Vostok",
341 "Arctic/Longyearbyen",
342 "Asia/Aden",
343 "Asia/Almaty",
344 "Asia/Amman",
345 "Asia/Anadyr",
346 "Asia/Aqtau",
347 "Asia/Aqtobe",
348 "Asia/Ashgabat",
349 "Asia/Ashkhabad",
350 "Asia/Atyrau",
351 "Asia/Baghdad",
352 "Asia/Bahrain",
353 "Asia/Baku",
354 "Asia/Bangkok",
355 "Asia/Barnaul",
356 "Asia/Beirut",
357 "Asia/Bishkek",
358 "Asia/Brunei",
359 "Asia/Calcutta",
360 "Asia/Chita",
361 "Asia/Choibalsan",
362 "Asia/Chongqing",
363 "Asia/Chungking",
364 "Asia/Colombo",
365 "Asia/Dacca",
366 "Asia/Damascus",
367 "Asia/Dhaka",
368 "Asia/Dili",
369 "Asia/Dubai",
370 "Asia/Dushanbe",
371 "Asia/Famagusta",
372 "Asia/Gaza",
373 "Asia/Harbin",
374 "Asia/Hebron",
375 "Asia/Ho_Chi_Minh",
376 "Asia/Hong_Kong",
377 "Asia/Hovd",
378 "Asia/Irkutsk",
379 "Asia/Istanbul",
380 "Asia/Jakarta",
381 "Asia/Jayapura",
382 "Asia/Jerusalem",
383 "Asia/Kabul",
384 "Asia/Kamchatka",
385 "Asia/Karachi",
386 "Asia/Kashgar",
387 "Asia/Kathmandu",
388 "Asia/Katmandu",
389 "Asia/Khandyga",
390 "Asia/Kolkata",
391 "Asia/Krasnoyarsk",
392 "Asia/Kuala_Lumpur",
393 "Asia/Kuching",
394 "Asia/Kuwait",
395 "Asia/Macao",
396 "Asia/Macau",
397 "Asia/Magadan",
398 "Asia/Makassar",
399 "Asia/Manila",
400 "Asia/Muscat",
401 "Asia/Nicosia",
402 "Asia/Novokuznetsk",
403 "Asia/Novosibirsk",
404 "Asia/Omsk",
405 "Asia/Oral",
406 "Asia/Phnom_Penh",
407 "Asia/Pontianak",
408 "Asia/Pyongyang",
409 "Asia/Qatar",
410 "Asia/Qostanay",
411 "Asia/Qyzylorda",
412 "Asia/Rangoon",
413 "Asia/Riyadh",
414 "Asia/Saigon",
415 "Asia/Sakhalin",
416 "Asia/Samarkand",
417 "Asia/Seoul",
418 "Asia/Shanghai",
419 "Asia/Singapore",
420 "Asia/Srednekolymsk",
421 "Asia/Taipei",
422 "Asia/Tashkent",
423 "Asia/Tbilisi",
424 "Asia/Tehran",
425 "Asia/Tel_Aviv",
426 "Asia/Thimbu",
427 "Asia/Thimphu",
428 "Asia/Tokyo",
429 "Asia/Tomsk",
430 "Asia/Ujung_Pandang",
431 "Asia/Ulaanbaatar",
432 "Asia/Ulan_Bator",
433 "Asia/Urumqi",
434 "Asia/Ust-Nera",
435 "Asia/Vientiane",
436 "Asia/Vladivostok",
437 "Asia/Yakutsk",
438 "Asia/Yangon",
439 "Asia/Yekaterinburg",
440 "Asia/Yerevan",
441 "Atlantic/Azores",
442 "Atlantic/Bermuda",
443 "Atlantic/Canary",
444 "Atlantic/Cape_Verde",
445 "Atlantic/Faeroe",
446 "Atlantic/Faroe",
447 "Atlantic/Jan_Mayen",
448 "Atlantic/Madeira",
449 "Atlantic/Reykjavik",
450 "Atlantic/South_Georgia",
451 "Atlantic/St_Helena",
452 "Atlantic/Stanley",
453 "Australia/ACT",
454 "Australia/Adelaide",
455 "Australia/Brisbane",
456 "Australia/Broken_Hill",
457 "Australia/Canberra",
458 "Australia/Currie",
459 "Australia/Darwin",
460 "Australia/Eucla",
461 "Australia/Hobart",
462 "Australia/LHI",
463 "Australia/Lindeman",
464 "Australia/Lord_Howe",
465 "Australia/Melbourne",
466 "Australia/NSW",
467 "Australia/North",
468 "Australia/Perth",
469 "Australia/Queensland",
470 "Australia/South",
471 "Australia/Sydney",
472 "Australia/Tasmania",
473 "Australia/Victoria",
474 "Australia/West",
475 "Australia/Yancowinna",
476 "Brazil/Acre",
477 "Brazil/DeNoronha",
478 "Brazil/East",
479 "Brazil/West",
480 "CET",
481 "CST6CDT",
482 "Canada/Atlantic",
483 "Canada/Central",
484 "Canada/Eastern",
485 "Canada/Mountain",
486 "Canada/Newfoundland",
487 "Canada/Pacific",
488 "Canada/Saskatchewan",
489 "Canada/Yukon",
490 "Chile/Continental",
491 "Chile/EasterIsland",
492 "Cuba",
493 "EET",
494 "EST",
495 "EST5EDT",
496 "Egypt",
497 "Eire",
498 "Etc/GMT",
499 "Etc/GMT+0",
500 "Etc/GMT+1",
501 "Etc/GMT+10",
502 "Etc/GMT+11",
503 "Etc/GMT+12",
504 "Etc/GMT+2",
505 "Etc/GMT+3",
506 "Etc/GMT+4",
507 "Etc/GMT+5",
508 "Etc/GMT+6",
509 "Etc/GMT+7",
510 "Etc/GMT+8",
511 "Etc/GMT+9",
512 "Etc/GMT-0",
513 "Etc/GMT-1",
514 "Etc/GMT-10",
515 "Etc/GMT-11",
516 "Etc/GMT-12",
517 "Etc/GMT-13",
518 "Etc/GMT-14",
519 "Etc/GMT-2",
520 "Etc/GMT-3",
521 "Etc/GMT-4",
522 "Etc/GMT-5",
523 "Etc/GMT-6",
524 "Etc/GMT-7",
525 "Etc/GMT-8",
526 "Etc/GMT-9",
527 "Etc/GMT0",
528 "Etc/Greenwich",
529 "Etc/UCT",
530 "Etc/UTC",
531 "Etc/Universal",
532 "Etc/Zulu",
533 "Europe/Amsterdam",
534 "Europe/Andorra",
535 "Europe/Astrakhan",
536 "Europe/Athens",
537 "Europe/Belfast",
538 "Europe/Belgrade",
539 "Europe/Berlin",
540 "Europe/Bratislava",
541 "Europe/Brussels",
542 "Europe/Bucharest",
543 "Europe/Budapest",
544 "Europe/Busingen",
545 "Europe/Chisinau",
546 "Europe/Copenhagen",
547 "Europe/Dublin",
548 "Europe/Gibraltar",
549 "Europe/Guernsey",
550 "Europe/Helsinki",
551 "Europe/Isle_of_Man",
552 "Europe/Istanbul",
553 "Europe/Jersey",
554 "Europe/Kaliningrad",
555 "Europe/Kiev",
556 "Europe/Kirov",
557 "Europe/Lisbon",
558 "Europe/Ljubljana",
559 "Europe/London",
560 "Europe/Luxembourg",
561 "Europe/Madrid",
562 "Europe/Malta",
563 "Europe/Mariehamn",
564 "Europe/Minsk",
565 "Europe/Monaco",
566 "Europe/Moscow",
567 "Europe/Nicosia",
568 "Europe/Oslo",
569 "Europe/Paris",
570 "Europe/Podgorica",
571 "Europe/Prague",
572 "Europe/Riga",
573 "Europe/Rome",
574 "Europe/Samara",
575 "Europe/San_Marino",
576 "Europe/Sarajevo",
577 "Europe/Saratov",
578 "Europe/Simferopol",
579 "Europe/Skopje",
580 "Europe/Sofia",
581 "Europe/Stockholm",
582 "Europe/Tallinn",
583 "Europe/Tirane",
584 "Europe/Tiraspol",
585 "Europe/Ulyanovsk",
586 "Europe/Uzhgorod",
587 "Europe/Vaduz",
588 "Europe/Vatican",
589 "Europe/Vienna",
590 "Europe/Vilnius",
591 "Europe/Volgograd",
592 "Europe/Warsaw",
593 "Europe/Zagreb",
594 "Europe/Zaporozhye",
595 "Europe/Zurich",
596 "GB",
597 "GB-Eire",
598 "GMT",
599 "GMT+0",
600 "GMT-0",
601 "GMT0",
602 "Greenwich",
603 "HST",
604 "Hongkong",
605 "Iceland",
606 "Indian/Antananarivo",
607 "Indian/Chagos",
608 "Indian/Christmas",
609 "Indian/Cocos",
610 "Indian/Comoro",
611 "Indian/Kerguelen",
612 "Indian/Mahe",
613 "Indian/Maldives",
614 "Indian/Mauritius",
615 "Indian/Mayotte",
616 "Indian/Reunion",
617 "Iran",
618 "Israel",
619 "Jamaica",
620 "Japan",
621 "Kwajalein",
622 "Libya",
623 "MET",
624 "MST",
625 "MST7MDT",
626 "Mexico/BajaNorte",
627 "Mexico/BajaSur",
628 "Mexico/General",
629 "NZ",
630 "NZ-CHAT",
631 "Navajo",
632 "PRC",
633 "PST8PDT",
634 "Pacific/Apia",
635 "Pacific/Auckland",
636 "Pacific/Bougainville",
637 "Pacific/Chatham",
638 "Pacific/Chuuk",
639 "Pacific/Easter",
640 "Pacific/Efate",
641 "Pacific/Enderbury",
642 "Pacific/Fakaofo",
643 "Pacific/Fiji",
644 "Pacific/Funafuti",
645 "Pacific/Galapagos",
646 "Pacific/Gambier",
647 "Pacific/Guadalcanal",
648 "Pacific/Guam",
649 "Pacific/Honolulu",
650 "Pacific/Johnston",
651 "Pacific/Kiritimati",
652 "Pacific/Kosrae",
653 "Pacific/Kwajalein",
654 "Pacific/Majuro",
655 "Pacific/Marquesas",
656 "Pacific/Midway",
657 "Pacific/Nauru",
658 "Pacific/Niue",
659 "Pacific/Norfolk",
660 "Pacific/Noumea",
661 "Pacific/Pago_Pago",
662 "Pacific/Palau",
663 "Pacific/Pitcairn",
664 "Pacific/Pohnpei",
665 "Pacific/Ponape",
666 "Pacific/Port_Moresby",
667 "Pacific/Rarotonga",
668 "Pacific/Saipan",
669 "Pacific/Samoa",
670 "Pacific/Tahiti",
671 "Pacific/Tarawa",
672 "Pacific/Tongatapu",
673 "Pacific/Truk",
674 "Pacific/Wake",
675 "Pacific/Wallis",
676 "Pacific/Yap",
677 "Poland",
678 "Portugal",
679 "ROC",
680 "ROK",
681 "Singapore",
682 "Turkey",
683 "UCT",
684 "US/Alaska",
685 "US/Aleutian",
686 "US/Arizona",
687 "US/Central",
688 "US/East-Indiana",
689 "US/Eastern",
690 "US/Hawaii",
691 "US/Indiana-Starke",
692 "US/Michigan",
693 "US/Mountain",
694 "US/Pacific",
695 "US/Samoa",
696 "UTC",
697 "Universal",
698 "W-SU",
699 "WET",
700 "Zulu",
701 nullptr};
702
AllTimeZoneNames()703 std::vector<std::string> AllTimeZoneNames() {
704 std::vector<std::string> names;
705 for (const char* const* namep = kTimeZoneNames; *namep != nullptr; ++namep) {
706 names.push_back(std::string("file:") + *namep);
707 }
708 assert(!names.empty());
709
710 std::mt19937 urbg(42); // a UniformRandomBitGenerator with fixed seed
711 std::shuffle(names.begin(), names.end(), urbg);
712 return names;
713 }
714
TestTimeZone()715 cctz::time_zone TestTimeZone() {
716 cctz::time_zone tz;
717 cctz::load_time_zone("America/Los_Angeles", &tz);
718 return tz;
719 }
720
BM_Zone_LoadUTCTimeZoneFirst(benchmark::State & state)721 void BM_Zone_LoadUTCTimeZoneFirst(benchmark::State& state) {
722 cctz::time_zone tz;
723 cctz::load_time_zone("UTC", &tz); // in case we're first
724 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
725 while (state.KeepRunning()) {
726 benchmark::DoNotOptimize(cctz::load_time_zone("UTC", &tz));
727 }
728 }
729 BENCHMARK(BM_Zone_LoadUTCTimeZoneFirst);
730
BM_Zone_LoadUTCTimeZoneLast(benchmark::State & state)731 void BM_Zone_LoadUTCTimeZoneLast(benchmark::State& state) {
732 cctz::time_zone tz;
733 for (const auto& name : AllTimeZoneNames()) {
734 cctz::load_time_zone(name, &tz); // prime cache
735 }
736 while (state.KeepRunning()) {
737 benchmark::DoNotOptimize(cctz::load_time_zone("UTC", &tz));
738 }
739 }
740 BENCHMARK(BM_Zone_LoadUTCTimeZoneLast);
741
BM_Zone_LoadTimeZoneFirst(benchmark::State & state)742 void BM_Zone_LoadTimeZoneFirst(benchmark::State& state) {
743 cctz::time_zone tz = cctz::utc_time_zone(); // in case we're first
744 const std::string name = "file:America/Los_Angeles";
745 while (state.KeepRunning()) {
746 state.PauseTiming();
747 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
748 state.ResumeTiming();
749 benchmark::DoNotOptimize(cctz::load_time_zone(name, &tz));
750 }
751 }
752 BENCHMARK(BM_Zone_LoadTimeZoneFirst);
753
BM_Zone_LoadTimeZoneCached(benchmark::State & state)754 void BM_Zone_LoadTimeZoneCached(benchmark::State& state) {
755 cctz::time_zone tz = cctz::utc_time_zone(); // in case we're first
756 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
757 const std::string name = "file:America/Los_Angeles";
758 cctz::load_time_zone(name, &tz); // prime cache
759 while (state.KeepRunning()) {
760 benchmark::DoNotOptimize(cctz::load_time_zone(name, &tz));
761 }
762 }
763 BENCHMARK(BM_Zone_LoadTimeZoneCached);
764
BM_Zone_LoadLocalTimeZoneCached(benchmark::State & state)765 void BM_Zone_LoadLocalTimeZoneCached(benchmark::State& state) {
766 cctz::utc_time_zone(); // in case we're first
767 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
768 cctz::local_time_zone(); // prime cache
769 while (state.KeepRunning()) {
770 benchmark::DoNotOptimize(cctz::local_time_zone());
771 }
772 }
773 BENCHMARK(BM_Zone_LoadLocalTimeZoneCached);
774
BM_Zone_LoadAllTimeZonesFirst(benchmark::State & state)775 void BM_Zone_LoadAllTimeZonesFirst(benchmark::State& state) {
776 cctz::time_zone tz;
777 const std::vector<std::string> names = AllTimeZoneNames();
778 for (auto index = names.size(); state.KeepRunning(); ++index) {
779 if (index == names.size()) {
780 index = 0;
781 }
782 if (index == 0) {
783 state.PauseTiming();
784 cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
785 state.ResumeTiming();
786 }
787 benchmark::DoNotOptimize(cctz::load_time_zone(names[index], &tz));
788 }
789 }
790 BENCHMARK(BM_Zone_LoadAllTimeZonesFirst);
791
BM_Zone_LoadAllTimeZonesCached(benchmark::State & state)792 void BM_Zone_LoadAllTimeZonesCached(benchmark::State& state) {
793 cctz::time_zone tz;
794 const std::vector<std::string> names = AllTimeZoneNames();
795 for (const auto& name : names) {
796 cctz::load_time_zone(name, &tz); // prime cache
797 }
798 for (auto index = names.size(); state.KeepRunning(); ++index) {
799 if (index == names.size()) {
800 index = 0;
801 }
802 benchmark::DoNotOptimize(cctz::load_time_zone(names[index], &tz));
803 }
804 }
805 BENCHMARK(BM_Zone_LoadAllTimeZonesCached);
806
BM_Zone_TimeZoneEqualityImplicit(benchmark::State & state)807 void BM_Zone_TimeZoneEqualityImplicit(benchmark::State& state) {
808 cctz::time_zone tz; // implicit UTC
809 while (state.KeepRunning()) {
810 benchmark::DoNotOptimize(tz == tz);
811 }
812 }
813 BENCHMARK(BM_Zone_TimeZoneEqualityImplicit);
814
BM_Zone_TimeZoneEqualityExplicit(benchmark::State & state)815 void BM_Zone_TimeZoneEqualityExplicit(benchmark::State& state) {
816 cctz::time_zone tz = cctz::utc_time_zone(); // explicit UTC
817 while (state.KeepRunning()) {
818 benchmark::DoNotOptimize(tz == tz);
819 }
820 }
821 BENCHMARK(BM_Zone_TimeZoneEqualityExplicit);
822
BM_Zone_UTCTimeZone(benchmark::State & state)823 void BM_Zone_UTCTimeZone(benchmark::State& state) {
824 cctz::time_zone tz;
825 while (state.KeepRunning()) {
826 benchmark::DoNotOptimize(cctz::utc_time_zone());
827 }
828 }
829 BENCHMARK(BM_Zone_UTCTimeZone);
830
831 // In each "ToCivil" benchmark we switch between two instants separated
832 // by at least one transition in order to defeat any internal caching of
833 // previous results (e.g., see local_time_hint_).
834 //
835 // The "UTC" variants use UTC instead of the Google/local time zone.
836
BM_Time_ToCivil_CCTZ(benchmark::State & state)837 void BM_Time_ToCivil_CCTZ(benchmark::State& state) {
838 const cctz::time_zone tz = TestTimeZone();
839 std::chrono::system_clock::time_point tp =
840 std::chrono::system_clock::from_time_t(1384569027);
841 std::chrono::system_clock::time_point tp2 =
842 std::chrono::system_clock::from_time_t(1418962578);
843 while (state.KeepRunning()) {
844 std::swap(tp, tp2);
845 tp += std::chrono::seconds(1);
846 benchmark::DoNotOptimize(cctz::convert(tp, tz));
847 }
848 }
849 BENCHMARK(BM_Time_ToCivil_CCTZ);
850
BM_Time_ToCivil_Libc(benchmark::State & state)851 void BM_Time_ToCivil_Libc(benchmark::State& state) {
852 // No timezone support, so just use localtime.
853 time_t t = 1384569027;
854 time_t t2 = 1418962578;
855 struct tm tm;
856 while (state.KeepRunning()) {
857 std::swap(t, t2);
858 t += 1;
859 #if defined(_WIN32) || defined(_WIN64)
860 benchmark::DoNotOptimize(localtime_s(&tm, &t));
861 #else
862 benchmark::DoNotOptimize(localtime_r(&t, &tm));
863 #endif
864 }
865 }
866 BENCHMARK(BM_Time_ToCivil_Libc);
867
BM_Time_ToCivilUTC_CCTZ(benchmark::State & state)868 void BM_Time_ToCivilUTC_CCTZ(benchmark::State& state) {
869 const cctz::time_zone tz = cctz::utc_time_zone();
870 std::chrono::system_clock::time_point tp =
871 std::chrono::system_clock::from_time_t(1384569027);
872 while (state.KeepRunning()) {
873 tp += std::chrono::seconds(1);
874 benchmark::DoNotOptimize(cctz::convert(tp, tz));
875 }
876 }
877 BENCHMARK(BM_Time_ToCivilUTC_CCTZ);
878
BM_Time_ToCivilUTC_Libc(benchmark::State & state)879 void BM_Time_ToCivilUTC_Libc(benchmark::State& state) {
880 time_t t = 1384569027;
881 struct tm tm;
882 while (state.KeepRunning()) {
883 t += 1;
884 #if defined(_WIN32) || defined(_WIN64)
885 benchmark::DoNotOptimize(gmtime_s(&tm, &t));
886 #else
887 benchmark::DoNotOptimize(gmtime_r(&t, &tm));
888 #endif
889 }
890 }
891 BENCHMARK(BM_Time_ToCivilUTC_Libc);
892
893 // In each "FromCivil" benchmark we switch between two YMDhms values
894 // separated by at least one transition in order to defeat any internal
895 // caching of previous results (e.g., see time_local_hint_).
896 //
897 // The "UTC" variants use UTC instead of the Google/local time zone.
898 // The "Day0" variants require normalization of the day of month.
899
BM_Time_FromCivil_CCTZ(benchmark::State & state)900 void BM_Time_FromCivil_CCTZ(benchmark::State& state) {
901 const cctz::time_zone tz = TestTimeZone();
902 int i = 0;
903 while (state.KeepRunning()) {
904 if ((i++ & 1) == 0) {
905 benchmark::DoNotOptimize(
906 cctz::convert(cctz::civil_second(2014, 12, 18, 20, 16, 18), tz));
907 } else {
908 benchmark::DoNotOptimize(
909 cctz::convert(cctz::civil_second(2013, 11, 15, 18, 30, 27), tz));
910 }
911 }
912 }
913 BENCHMARK(BM_Time_FromCivil_CCTZ);
914
BM_Time_FromCivil_Libc(benchmark::State & state)915 void BM_Time_FromCivil_Libc(benchmark::State& state) {
916 // No timezone support, so just use localtime.
917 int i = 0;
918 while (state.KeepRunning()) {
919 struct tm tm;
920 if ((i++ & 1) == 0) {
921 tm.tm_year = 2014 - 1900;
922 tm.tm_mon = 12 - 1;
923 tm.tm_mday = 18;
924 tm.tm_hour = 20;
925 tm.tm_min = 16;
926 tm.tm_sec = 18;
927 } else {
928 tm.tm_year = 2013 - 1900;
929 tm.tm_mon = 11 - 1;
930 tm.tm_mday = 15;
931 tm.tm_hour = 18;
932 tm.tm_min = 30;
933 tm.tm_sec = 27;
934 }
935 tm.tm_isdst = -1;
936 benchmark::DoNotOptimize(mktime(&tm));
937 }
938 }
939 BENCHMARK(BM_Time_FromCivil_Libc);
940
BM_Time_FromCivilUTC_CCTZ(benchmark::State & state)941 void BM_Time_FromCivilUTC_CCTZ(benchmark::State& state) {
942 const cctz::time_zone tz = cctz::utc_time_zone();
943 while (state.KeepRunning()) {
944 benchmark::DoNotOptimize(
945 cctz::convert(cctz::civil_second(2014, 12, 18, 20, 16, 18), tz));
946 }
947 }
948 BENCHMARK(BM_Time_FromCivilUTC_CCTZ);
949
950 // There is no BM_Time_FromCivilUTC_Libc.
951
BM_Time_FromCivilDay0_CCTZ(benchmark::State & state)952 void BM_Time_FromCivilDay0_CCTZ(benchmark::State& state) {
953 const cctz::time_zone tz = TestTimeZone();
954 int i = 0;
955 while (state.KeepRunning()) {
956 if ((i++ & 1) == 0) {
957 benchmark::DoNotOptimize(
958 cctz::convert(cctz::civil_second(2014, 12, 0, 20, 16, 18), tz));
959 } else {
960 benchmark::DoNotOptimize(
961 cctz::convert(cctz::civil_second(2013, 11, 0, 18, 30, 27), tz));
962 }
963 }
964 }
965 BENCHMARK(BM_Time_FromCivilDay0_CCTZ);
966
BM_Time_FromCivilDay0_Libc(benchmark::State & state)967 void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
968 // No timezone support, so just use localtime.
969 int i = 0;
970 while (state.KeepRunning()) {
971 struct tm tm;
972 if ((i++ & 1) == 0) {
973 tm.tm_year = 2014 - 1900;
974 tm.tm_mon = 12 - 1;
975 tm.tm_mday = 0;
976 tm.tm_hour = 20;
977 tm.tm_min = 16;
978 tm.tm_sec = 18;
979 } else {
980 tm.tm_year = 2013 - 1900;
981 tm.tm_mon = 11 - 1;
982 tm.tm_mday = 0;
983 tm.tm_hour = 18;
984 tm.tm_min = 30;
985 tm.tm_sec = 27;
986 }
987 tm.tm_isdst = -1;
988 benchmark::DoNotOptimize(mktime(&tm));
989 }
990 }
991 BENCHMARK(BM_Time_FromCivilDay0_Libc);
992
993 const char* const kFormats[] = {
994 RFC1123_full, // 0
995 RFC1123_no_wday, // 1
996 RFC3339_full, // 2
997 RFC3339_sec, // 3
998 "%Y-%m-%d%ET%H:%M:%S", // 4
999 "%Y-%m-%d", // 5
1000 };
1001 const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
1002
BM_Format_FormatTime(benchmark::State & state)1003 void BM_Format_FormatTime(benchmark::State& state) {
1004 const std::string fmt = kFormats[state.range(0)];
1005 state.SetLabel(fmt);
1006 const cctz::time_zone tz = TestTimeZone();
1007 const std::chrono::system_clock::time_point tp =
1008 cctz::convert(cctz::civil_second(1977, 6, 28, 9, 8, 7), tz) +
1009 std::chrono::microseconds(1);
1010 while (state.KeepRunning()) {
1011 benchmark::DoNotOptimize(cctz::format(fmt, tp, tz));
1012 }
1013 }
1014 BENCHMARK(BM_Format_FormatTime)->DenseRange(0, kNumFormats - 1);
1015
BM_Format_ParseTime(benchmark::State & state)1016 void BM_Format_ParseTime(benchmark::State& state) {
1017 const std::string fmt = kFormats[state.range(0)];
1018 state.SetLabel(fmt);
1019 const cctz::time_zone tz = TestTimeZone();
1020 std::chrono::system_clock::time_point tp =
1021 cctz::convert(cctz::civil_second(1977, 6, 28, 9, 8, 7), tz) +
1022 std::chrono::microseconds(1);
1023 const std::string when = cctz::format(fmt, tp, tz);
1024 while (state.KeepRunning()) {
1025 benchmark::DoNotOptimize(cctz::parse(fmt, when, tz, &tp));
1026 }
1027 }
1028 BENCHMARK(BM_Format_ParseTime)->DenseRange(0, kNumFormats - 1);
1029
1030 } // namespace
1031