cctz_benchmark.cc
Go to the documentation of this file.
00001 // Copyright 2016 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //   https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 //   Unless required by applicable law or agreed to in writing, software
00010 //   distributed under the License is distributed on an "AS IS" BASIS,
00011 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 //   See the License for the specific language governing permissions and
00013 //   limitations under the License.
00014 
00015 #include <algorithm>
00016 #include <cassert>
00017 #include <chrono>
00018 #include <ctime>
00019 #include <random>
00020 #include <string>
00021 #include <vector>
00022 
00023 #include "benchmark/benchmark.h"
00024 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
00025 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
00026 #include "time_zone_impl.h"
00027 
00028 namespace {
00029 
00030 namespace cctz = absl::time_internal::cctz;
00031 
00032 void BM_Difference_Days(benchmark::State& state) {
00033   const cctz::civil_day c(2014, 8, 22);
00034   const cctz::civil_day epoch(1970, 1, 1);
00035   while (state.KeepRunning()) {
00036     benchmark::DoNotOptimize(c - epoch);
00037   }
00038 }
00039 BENCHMARK(BM_Difference_Days);
00040 
00041 void BM_Step_Days(benchmark::State& state) {
00042   const cctz::civil_day kStart(2014, 8, 22);
00043   cctz::civil_day c = kStart;
00044   while (state.KeepRunning()) {
00045     benchmark::DoNotOptimize(++c);
00046   }
00047 }
00048 BENCHMARK(BM_Step_Days);
00049 
00050 void BM_GetWeekday(benchmark::State& state) {
00051   const cctz::civil_day c(2014, 8, 22);
00052   while (state.KeepRunning()) {
00053     benchmark::DoNotOptimize(cctz::get_weekday(c));
00054   }
00055 }
00056 BENCHMARK(BM_GetWeekday);
00057 
00058 void BM_NextWeekday(benchmark::State& state) {
00059   const cctz::civil_day kStart(2014, 8, 22);
00060   const cctz::civil_day kDays[7] = {
00061       kStart + 0, kStart + 1, kStart + 2, kStart + 3,
00062       kStart + 4, kStart + 5, kStart + 6,
00063   };
00064   const cctz::weekday kWeekdays[7] = {
00065       cctz::weekday::monday,   cctz::weekday::tuesday, cctz::weekday::wednesday,
00066       cctz::weekday::thursday, cctz::weekday::friday,  cctz::weekday::saturday,
00067       cctz::weekday::sunday,
00068   };
00069   while (state.KeepRunningBatch(7 * 7)) {
00070     for (const auto from : kDays) {
00071       for (const auto to : kWeekdays) {
00072         benchmark::DoNotOptimize(cctz::next_weekday(from, to));
00073       }
00074     }
00075   }
00076 }
00077 BENCHMARK(BM_NextWeekday);
00078 
00079 void BM_PrevWeekday(benchmark::State& state) {
00080   const cctz::civil_day kStart(2014, 8, 22);
00081   const cctz::civil_day kDays[7] = {
00082       kStart + 0, kStart + 1, kStart + 2, kStart + 3,
00083       kStart + 4, kStart + 5, kStart + 6,
00084   };
00085   const cctz::weekday kWeekdays[7] = {
00086       cctz::weekday::monday,   cctz::weekday::tuesday, cctz::weekday::wednesday,
00087       cctz::weekday::thursday, cctz::weekday::friday,  cctz::weekday::saturday,
00088       cctz::weekday::sunday,
00089   };
00090   while (state.KeepRunningBatch(7 * 7)) {
00091     for (const auto from : kDays) {
00092       for (const auto to : kWeekdays) {
00093         benchmark::DoNotOptimize(cctz::prev_weekday(from, to));
00094       }
00095     }
00096   }
00097 }
00098 BENCHMARK(BM_PrevWeekday);
00099 
00100 const char RFC3339_full[] = "%Y-%m-%dT%H:%M:%E*S%Ez";
00101 const char RFC3339_sec[] = "%Y-%m-%dT%H:%M:%S%Ez";
00102 
00103 const char RFC1123_full[] = "%a, %d %b %Y %H:%M:%S %z";
00104 const char RFC1123_no_wday[] = "%d %b %Y %H:%M:%S %z";
00105 
00106 // A list of known time-zone names.
00107 // TODO: Refactor with src/time_zone_lookup_test.cc.
00108 const char* const kTimeZoneNames[] = {
00109   "Africa/Abidjan",
00110   "Africa/Accra",
00111   "Africa/Addis_Ababa",
00112   "Africa/Algiers",
00113   "Africa/Asmara",
00114   "Africa/Asmera",
00115   "Africa/Bamako",
00116   "Africa/Bangui",
00117   "Africa/Banjul",
00118   "Africa/Bissau",
00119   "Africa/Blantyre",
00120   "Africa/Brazzaville",
00121   "Africa/Bujumbura",
00122   "Africa/Cairo",
00123   "Africa/Casablanca",
00124   "Africa/Ceuta",
00125   "Africa/Conakry",
00126   "Africa/Dakar",
00127   "Africa/Dar_es_Salaam",
00128   "Africa/Djibouti",
00129   "Africa/Douala",
00130   "Africa/El_Aaiun",
00131   "Africa/Freetown",
00132   "Africa/Gaborone",
00133   "Africa/Harare",
00134   "Africa/Johannesburg",
00135   "Africa/Juba",
00136   "Africa/Kampala",
00137   "Africa/Khartoum",
00138   "Africa/Kigali",
00139   "Africa/Kinshasa",
00140   "Africa/Lagos",
00141   "Africa/Libreville",
00142   "Africa/Lome",
00143   "Africa/Luanda",
00144   "Africa/Lubumbashi",
00145   "Africa/Lusaka",
00146   "Africa/Malabo",
00147   "Africa/Maputo",
00148   "Africa/Maseru",
00149   "Africa/Mbabane",
00150   "Africa/Mogadishu",
00151   "Africa/Monrovia",
00152   "Africa/Nairobi",
00153   "Africa/Ndjamena",
00154   "Africa/Niamey",
00155   "Africa/Nouakchott",
00156   "Africa/Ouagadougou",
00157   "Africa/Porto-Novo",
00158   "Africa/Sao_Tome",
00159   "Africa/Timbuktu",
00160   "Africa/Tripoli",
00161   "Africa/Tunis",
00162   "Africa/Windhoek",
00163   "America/Adak",
00164   "America/Anchorage",
00165   "America/Anguilla",
00166   "America/Antigua",
00167   "America/Araguaina",
00168   "America/Argentina/Buenos_Aires",
00169   "America/Argentina/Catamarca",
00170   "America/Argentina/ComodRivadavia",
00171   "America/Argentina/Cordoba",
00172   "America/Argentina/Jujuy",
00173   "America/Argentina/La_Rioja",
00174   "America/Argentina/Mendoza",
00175   "America/Argentina/Rio_Gallegos",
00176   "America/Argentina/Salta",
00177   "America/Argentina/San_Juan",
00178   "America/Argentina/San_Luis",
00179   "America/Argentina/Tucuman",
00180   "America/Argentina/Ushuaia",
00181   "America/Aruba",
00182   "America/Asuncion",
00183   "America/Atikokan",
00184   "America/Atka",
00185   "America/Bahia",
00186   "America/Bahia_Banderas",
00187   "America/Barbados",
00188   "America/Belem",
00189   "America/Belize",
00190   "America/Blanc-Sablon",
00191   "America/Boa_Vista",
00192   "America/Bogota",
00193   "America/Boise",
00194   "America/Buenos_Aires",
00195   "America/Cambridge_Bay",
00196   "America/Campo_Grande",
00197   "America/Cancun",
00198   "America/Caracas",
00199   "America/Catamarca",
00200   "America/Cayenne",
00201   "America/Cayman",
00202   "America/Chicago",
00203   "America/Chihuahua",
00204   "America/Coral_Harbour",
00205   "America/Cordoba",
00206   "America/Costa_Rica",
00207   "America/Creston",
00208   "America/Cuiaba",
00209   "America/Curacao",
00210   "America/Danmarkshavn",
00211   "America/Dawson",
00212   "America/Dawson_Creek",
00213   "America/Denver",
00214   "America/Detroit",
00215   "America/Dominica",
00216   "America/Edmonton",
00217   "America/Eirunepe",
00218   "America/El_Salvador",
00219   "America/Ensenada",
00220   "America/Fort_Nelson",
00221   "America/Fort_Wayne",
00222   "America/Fortaleza",
00223   "America/Glace_Bay",
00224   "America/Godthab",
00225   "America/Goose_Bay",
00226   "America/Grand_Turk",
00227   "America/Grenada",
00228   "America/Guadeloupe",
00229   "America/Guatemala",
00230   "America/Guayaquil",
00231   "America/Guyana",
00232   "America/Halifax",
00233   "America/Havana",
00234   "America/Hermosillo",
00235   "America/Indiana/Indianapolis",
00236   "America/Indiana/Knox",
00237   "America/Indiana/Marengo",
00238   "America/Indiana/Petersburg",
00239   "America/Indiana/Tell_City",
00240   "America/Indiana/Vevay",
00241   "America/Indiana/Vincennes",
00242   "America/Indiana/Winamac",
00243   "America/Indianapolis",
00244   "America/Inuvik",
00245   "America/Iqaluit",
00246   "America/Jamaica",
00247   "America/Jujuy",
00248   "America/Juneau",
00249   "America/Kentucky/Louisville",
00250   "America/Kentucky/Monticello",
00251   "America/Knox_IN",
00252   "America/Kralendijk",
00253   "America/La_Paz",
00254   "America/Lima",
00255   "America/Los_Angeles",
00256   "America/Louisville",
00257   "America/Lower_Princes",
00258   "America/Maceio",
00259   "America/Managua",
00260   "America/Manaus",
00261   "America/Marigot",
00262   "America/Martinique",
00263   "America/Matamoros",
00264   "America/Mazatlan",
00265   "America/Mendoza",
00266   "America/Menominee",
00267   "America/Merida",
00268   "America/Metlakatla",
00269   "America/Mexico_City",
00270   "America/Miquelon",
00271   "America/Moncton",
00272   "America/Monterrey",
00273   "America/Montevideo",
00274   "America/Montreal",
00275   "America/Montserrat",
00276   "America/Nassau",
00277   "America/New_York",
00278   "America/Nipigon",
00279   "America/Nome",
00280   "America/Noronha",
00281   "America/North_Dakota/Beulah",
00282   "America/North_Dakota/Center",
00283   "America/North_Dakota/New_Salem",
00284   "America/Ojinaga",
00285   "America/Panama",
00286   "America/Pangnirtung",
00287   "America/Paramaribo",
00288   "America/Phoenix",
00289   "America/Port-au-Prince",
00290   "America/Port_of_Spain",
00291   "America/Porto_Acre",
00292   "America/Porto_Velho",
00293   "America/Puerto_Rico",
00294   "America/Punta_Arenas",
00295   "America/Rainy_River",
00296   "America/Rankin_Inlet",
00297   "America/Recife",
00298   "America/Regina",
00299   "America/Resolute",
00300   "America/Rio_Branco",
00301   "America/Rosario",
00302   "America/Santa_Isabel",
00303   "America/Santarem",
00304   "America/Santiago",
00305   "America/Santo_Domingo",
00306   "America/Sao_Paulo",
00307   "America/Scoresbysund",
00308   "America/Shiprock",
00309   "America/Sitka",
00310   "America/St_Barthelemy",
00311   "America/St_Johns",
00312   "America/St_Kitts",
00313   "America/St_Lucia",
00314   "America/St_Thomas",
00315   "America/St_Vincent",
00316   "America/Swift_Current",
00317   "America/Tegucigalpa",
00318   "America/Thule",
00319   "America/Thunder_Bay",
00320   "America/Tijuana",
00321   "America/Toronto",
00322   "America/Tortola",
00323   "America/Vancouver",
00324   "America/Virgin",
00325   "America/Whitehorse",
00326   "America/Winnipeg",
00327   "America/Yakutat",
00328   "America/Yellowknife",
00329   "Antarctica/Casey",
00330   "Antarctica/Davis",
00331   "Antarctica/DumontDUrville",
00332   "Antarctica/Macquarie",
00333   "Antarctica/Mawson",
00334   "Antarctica/McMurdo",
00335   "Antarctica/Palmer",
00336   "Antarctica/Rothera",
00337   "Antarctica/South_Pole",
00338   "Antarctica/Syowa",
00339   "Antarctica/Troll",
00340   "Antarctica/Vostok",
00341   "Arctic/Longyearbyen",
00342   "Asia/Aden",
00343   "Asia/Almaty",
00344   "Asia/Amman",
00345   "Asia/Anadyr",
00346   "Asia/Aqtau",
00347   "Asia/Aqtobe",
00348   "Asia/Ashgabat",
00349   "Asia/Ashkhabad",
00350   "Asia/Atyrau",
00351   "Asia/Baghdad",
00352   "Asia/Bahrain",
00353   "Asia/Baku",
00354   "Asia/Bangkok",
00355   "Asia/Barnaul",
00356   "Asia/Beirut",
00357   "Asia/Bishkek",
00358   "Asia/Brunei",
00359   "Asia/Calcutta",
00360   "Asia/Chita",
00361   "Asia/Choibalsan",
00362   "Asia/Chongqing",
00363   "Asia/Chungking",
00364   "Asia/Colombo",
00365   "Asia/Dacca",
00366   "Asia/Damascus",
00367   "Asia/Dhaka",
00368   "Asia/Dili",
00369   "Asia/Dubai",
00370   "Asia/Dushanbe",
00371   "Asia/Famagusta",
00372   "Asia/Gaza",
00373   "Asia/Harbin",
00374   "Asia/Hebron",
00375   "Asia/Ho_Chi_Minh",
00376   "Asia/Hong_Kong",
00377   "Asia/Hovd",
00378   "Asia/Irkutsk",
00379   "Asia/Istanbul",
00380   "Asia/Jakarta",
00381   "Asia/Jayapura",
00382   "Asia/Jerusalem",
00383   "Asia/Kabul",
00384   "Asia/Kamchatka",
00385   "Asia/Karachi",
00386   "Asia/Kashgar",
00387   "Asia/Kathmandu",
00388   "Asia/Katmandu",
00389   "Asia/Khandyga",
00390   "Asia/Kolkata",
00391   "Asia/Krasnoyarsk",
00392   "Asia/Kuala_Lumpur",
00393   "Asia/Kuching",
00394   "Asia/Kuwait",
00395   "Asia/Macao",
00396   "Asia/Macau",
00397   "Asia/Magadan",
00398   "Asia/Makassar",
00399   "Asia/Manila",
00400   "Asia/Muscat",
00401   "Asia/Nicosia",
00402   "Asia/Novokuznetsk",
00403   "Asia/Novosibirsk",
00404   "Asia/Omsk",
00405   "Asia/Oral",
00406   "Asia/Phnom_Penh",
00407   "Asia/Pontianak",
00408   "Asia/Pyongyang",
00409   "Asia/Qatar",
00410   "Asia/Qostanay",
00411   "Asia/Qyzylorda",
00412   "Asia/Rangoon",
00413   "Asia/Riyadh",
00414   "Asia/Saigon",
00415   "Asia/Sakhalin",
00416   "Asia/Samarkand",
00417   "Asia/Seoul",
00418   "Asia/Shanghai",
00419   "Asia/Singapore",
00420   "Asia/Srednekolymsk",
00421   "Asia/Taipei",
00422   "Asia/Tashkent",
00423   "Asia/Tbilisi",
00424   "Asia/Tehran",
00425   "Asia/Tel_Aviv",
00426   "Asia/Thimbu",
00427   "Asia/Thimphu",
00428   "Asia/Tokyo",
00429   "Asia/Tomsk",
00430   "Asia/Ujung_Pandang",
00431   "Asia/Ulaanbaatar",
00432   "Asia/Ulan_Bator",
00433   "Asia/Urumqi",
00434   "Asia/Ust-Nera",
00435   "Asia/Vientiane",
00436   "Asia/Vladivostok",
00437   "Asia/Yakutsk",
00438   "Asia/Yangon",
00439   "Asia/Yekaterinburg",
00440   "Asia/Yerevan",
00441   "Atlantic/Azores",
00442   "Atlantic/Bermuda",
00443   "Atlantic/Canary",
00444   "Atlantic/Cape_Verde",
00445   "Atlantic/Faeroe",
00446   "Atlantic/Faroe",
00447   "Atlantic/Jan_Mayen",
00448   "Atlantic/Madeira",
00449   "Atlantic/Reykjavik",
00450   "Atlantic/South_Georgia",
00451   "Atlantic/St_Helena",
00452   "Atlantic/Stanley",
00453   "Australia/ACT",
00454   "Australia/Adelaide",
00455   "Australia/Brisbane",
00456   "Australia/Broken_Hill",
00457   "Australia/Canberra",
00458   "Australia/Currie",
00459   "Australia/Darwin",
00460   "Australia/Eucla",
00461   "Australia/Hobart",
00462   "Australia/LHI",
00463   "Australia/Lindeman",
00464   "Australia/Lord_Howe",
00465   "Australia/Melbourne",
00466   "Australia/NSW",
00467   "Australia/North",
00468   "Australia/Perth",
00469   "Australia/Queensland",
00470   "Australia/South",
00471   "Australia/Sydney",
00472   "Australia/Tasmania",
00473   "Australia/Victoria",
00474   "Australia/West",
00475   "Australia/Yancowinna",
00476   "Brazil/Acre",
00477   "Brazil/DeNoronha",
00478   "Brazil/East",
00479   "Brazil/West",
00480   "CET",
00481   "CST6CDT",
00482   "Canada/Atlantic",
00483   "Canada/Central",
00484   "Canada/Eastern",
00485   "Canada/Mountain",
00486   "Canada/Newfoundland",
00487   "Canada/Pacific",
00488   "Canada/Saskatchewan",
00489   "Canada/Yukon",
00490   "Chile/Continental",
00491   "Chile/EasterIsland",
00492   "Cuba",
00493   "EET",
00494   "EST",
00495   "EST5EDT",
00496   "Egypt",
00497   "Eire",
00498   "Etc/GMT",
00499   "Etc/GMT+0",
00500   "Etc/GMT+1",
00501   "Etc/GMT+10",
00502   "Etc/GMT+11",
00503   "Etc/GMT+12",
00504   "Etc/GMT+2",
00505   "Etc/GMT+3",
00506   "Etc/GMT+4",
00507   "Etc/GMT+5",
00508   "Etc/GMT+6",
00509   "Etc/GMT+7",
00510   "Etc/GMT+8",
00511   "Etc/GMT+9",
00512   "Etc/GMT-0",
00513   "Etc/GMT-1",
00514   "Etc/GMT-10",
00515   "Etc/GMT-11",
00516   "Etc/GMT-12",
00517   "Etc/GMT-13",
00518   "Etc/GMT-14",
00519   "Etc/GMT-2",
00520   "Etc/GMT-3",
00521   "Etc/GMT-4",
00522   "Etc/GMT-5",
00523   "Etc/GMT-6",
00524   "Etc/GMT-7",
00525   "Etc/GMT-8",
00526   "Etc/GMT-9",
00527   "Etc/GMT0",
00528   "Etc/Greenwich",
00529   "Etc/UCT",
00530   "Etc/UTC",
00531   "Etc/Universal",
00532   "Etc/Zulu",
00533   "Europe/Amsterdam",
00534   "Europe/Andorra",
00535   "Europe/Astrakhan",
00536   "Europe/Athens",
00537   "Europe/Belfast",
00538   "Europe/Belgrade",
00539   "Europe/Berlin",
00540   "Europe/Bratislava",
00541   "Europe/Brussels",
00542   "Europe/Bucharest",
00543   "Europe/Budapest",
00544   "Europe/Busingen",
00545   "Europe/Chisinau",
00546   "Europe/Copenhagen",
00547   "Europe/Dublin",
00548   "Europe/Gibraltar",
00549   "Europe/Guernsey",
00550   "Europe/Helsinki",
00551   "Europe/Isle_of_Man",
00552   "Europe/Istanbul",
00553   "Europe/Jersey",
00554   "Europe/Kaliningrad",
00555   "Europe/Kiev",
00556   "Europe/Kirov",
00557   "Europe/Lisbon",
00558   "Europe/Ljubljana",
00559   "Europe/London",
00560   "Europe/Luxembourg",
00561   "Europe/Madrid",
00562   "Europe/Malta",
00563   "Europe/Mariehamn",
00564   "Europe/Minsk",
00565   "Europe/Monaco",
00566   "Europe/Moscow",
00567   "Europe/Nicosia",
00568   "Europe/Oslo",
00569   "Europe/Paris",
00570   "Europe/Podgorica",
00571   "Europe/Prague",
00572   "Europe/Riga",
00573   "Europe/Rome",
00574   "Europe/Samara",
00575   "Europe/San_Marino",
00576   "Europe/Sarajevo",
00577   "Europe/Saratov",
00578   "Europe/Simferopol",
00579   "Europe/Skopje",
00580   "Europe/Sofia",
00581   "Europe/Stockholm",
00582   "Europe/Tallinn",
00583   "Europe/Tirane",
00584   "Europe/Tiraspol",
00585   "Europe/Ulyanovsk",
00586   "Europe/Uzhgorod",
00587   "Europe/Vaduz",
00588   "Europe/Vatican",
00589   "Europe/Vienna",
00590   "Europe/Vilnius",
00591   "Europe/Volgograd",
00592   "Europe/Warsaw",
00593   "Europe/Zagreb",
00594   "Europe/Zaporozhye",
00595   "Europe/Zurich",
00596   "GB",
00597   "GB-Eire",
00598   "GMT",
00599   "GMT+0",
00600   "GMT-0",
00601   "GMT0",
00602   "Greenwich",
00603   "HST",
00604   "Hongkong",
00605   "Iceland",
00606   "Indian/Antananarivo",
00607   "Indian/Chagos",
00608   "Indian/Christmas",
00609   "Indian/Cocos",
00610   "Indian/Comoro",
00611   "Indian/Kerguelen",
00612   "Indian/Mahe",
00613   "Indian/Maldives",
00614   "Indian/Mauritius",
00615   "Indian/Mayotte",
00616   "Indian/Reunion",
00617   "Iran",
00618   "Israel",
00619   "Jamaica",
00620   "Japan",
00621   "Kwajalein",
00622   "Libya",
00623   "MET",
00624   "MST",
00625   "MST7MDT",
00626   "Mexico/BajaNorte",
00627   "Mexico/BajaSur",
00628   "Mexico/General",
00629   "NZ",
00630   "NZ-CHAT",
00631   "Navajo",
00632   "PRC",
00633   "PST8PDT",
00634   "Pacific/Apia",
00635   "Pacific/Auckland",
00636   "Pacific/Bougainville",
00637   "Pacific/Chatham",
00638   "Pacific/Chuuk",
00639   "Pacific/Easter",
00640   "Pacific/Efate",
00641   "Pacific/Enderbury",
00642   "Pacific/Fakaofo",
00643   "Pacific/Fiji",
00644   "Pacific/Funafuti",
00645   "Pacific/Galapagos",
00646   "Pacific/Gambier",
00647   "Pacific/Guadalcanal",
00648   "Pacific/Guam",
00649   "Pacific/Honolulu",
00650   "Pacific/Johnston",
00651   "Pacific/Kiritimati",
00652   "Pacific/Kosrae",
00653   "Pacific/Kwajalein",
00654   "Pacific/Majuro",
00655   "Pacific/Marquesas",
00656   "Pacific/Midway",
00657   "Pacific/Nauru",
00658   "Pacific/Niue",
00659   "Pacific/Norfolk",
00660   "Pacific/Noumea",
00661   "Pacific/Pago_Pago",
00662   "Pacific/Palau",
00663   "Pacific/Pitcairn",
00664   "Pacific/Pohnpei",
00665   "Pacific/Ponape",
00666   "Pacific/Port_Moresby",
00667   "Pacific/Rarotonga",
00668   "Pacific/Saipan",
00669   "Pacific/Samoa",
00670   "Pacific/Tahiti",
00671   "Pacific/Tarawa",
00672   "Pacific/Tongatapu",
00673   "Pacific/Truk",
00674   "Pacific/Wake",
00675   "Pacific/Wallis",
00676   "Pacific/Yap",
00677   "Poland",
00678   "Portugal",
00679   "ROC",
00680   "ROK",
00681   "Singapore",
00682   "Turkey",
00683   "UCT",
00684   "US/Alaska",
00685   "US/Aleutian",
00686   "US/Arizona",
00687   "US/Central",
00688   "US/East-Indiana",
00689   "US/Eastern",
00690   "US/Hawaii",
00691   "US/Indiana-Starke",
00692   "US/Michigan",
00693   "US/Mountain",
00694   "US/Pacific",
00695   "US/Samoa",
00696   "UTC",
00697   "Universal",
00698   "W-SU",
00699   "WET",
00700   "Zulu",
00701   nullptr
00702 };
00703 
00704 std::vector<std::string> AllTimeZoneNames() {
00705   std::vector<std::string> names;
00706   for (const char* const* namep = kTimeZoneNames; *namep != nullptr; ++namep) {
00707     names.push_back(std::string("file:") + *namep);
00708   }
00709   assert(!names.empty());
00710 
00711   std::mt19937 urbg(42);  // a UniformRandomBitGenerator with fixed seed
00712   std::shuffle(names.begin(), names.end(), urbg);
00713   return names;
00714 }
00715 
00716 cctz::time_zone TestTimeZone() {
00717   cctz::time_zone tz;
00718   cctz::load_time_zone("America/Los_Angeles", &tz);
00719   return tz;
00720 }
00721 
00722 void BM_Zone_LoadUTCTimeZoneFirst(benchmark::State& state) {
00723   cctz::time_zone tz;
00724   cctz::load_time_zone("UTC", &tz);  // in case we're first
00725   cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
00726   while (state.KeepRunning()) {
00727     benchmark::DoNotOptimize(cctz::load_time_zone("UTC", &tz));
00728   }
00729 }
00730 BENCHMARK(BM_Zone_LoadUTCTimeZoneFirst);
00731 
00732 void BM_Zone_LoadUTCTimeZoneLast(benchmark::State& state) {
00733   cctz::time_zone tz;
00734   for (const auto& name : AllTimeZoneNames()) {
00735     cctz::load_time_zone(name, &tz);  // prime cache
00736   }
00737   while (state.KeepRunning()) {
00738     benchmark::DoNotOptimize(cctz::load_time_zone("UTC", &tz));
00739   }
00740 }
00741 BENCHMARK(BM_Zone_LoadUTCTimeZoneLast);
00742 
00743 void BM_Zone_LoadTimeZoneFirst(benchmark::State& state) {
00744   cctz::time_zone tz = cctz::utc_time_zone();  // in case we're first
00745   const std::string name = "file:America/Los_Angeles";
00746   while (state.KeepRunning()) {
00747     state.PauseTiming();
00748     cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
00749     state.ResumeTiming();
00750     benchmark::DoNotOptimize(cctz::load_time_zone(name, &tz));
00751   }
00752 }
00753 BENCHMARK(BM_Zone_LoadTimeZoneFirst);
00754 
00755 void BM_Zone_LoadTimeZoneCached(benchmark::State& state) {
00756   cctz::time_zone tz = cctz::utc_time_zone();  // in case we're first
00757   cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
00758   const std::string name = "file:America/Los_Angeles";
00759   cctz::load_time_zone(name, &tz);  // prime cache
00760   while (state.KeepRunning()) {
00761     benchmark::DoNotOptimize(cctz::load_time_zone(name, &tz));
00762   }
00763 }
00764 BENCHMARK(BM_Zone_LoadTimeZoneCached);
00765 
00766 void BM_Zone_LoadLocalTimeZoneCached(benchmark::State& state) {
00767   cctz::utc_time_zone();  // in case we're first
00768   cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
00769   cctz::local_time_zone();  // prime cache
00770   while (state.KeepRunning()) {
00771     benchmark::DoNotOptimize(cctz::local_time_zone());
00772   }
00773 }
00774 BENCHMARK(BM_Zone_LoadLocalTimeZoneCached);
00775 
00776 void BM_Zone_LoadAllTimeZonesFirst(benchmark::State& state) {
00777   cctz::time_zone tz;
00778   const std::vector<std::string> names = AllTimeZoneNames();
00779   for (auto index = names.size(); state.KeepRunning(); ++index) {
00780     if (index == names.size()) {
00781       index = 0;
00782     }
00783     if (index == 0) {
00784       state.PauseTiming();
00785       cctz::time_zone::Impl::ClearTimeZoneMapTestOnly();
00786       state.ResumeTiming();
00787     }
00788     benchmark::DoNotOptimize(cctz::load_time_zone(names[index], &tz));
00789   }
00790 }
00791 BENCHMARK(BM_Zone_LoadAllTimeZonesFirst);
00792 
00793 void BM_Zone_LoadAllTimeZonesCached(benchmark::State& state) {
00794   cctz::time_zone tz;
00795   const std::vector<std::string> names = AllTimeZoneNames();
00796   for (const auto& name : names) {
00797     cctz::load_time_zone(name, &tz);  // prime cache
00798   }
00799   for (auto index = names.size(); state.KeepRunning(); ++index) {
00800     if (index == names.size()) {
00801       index = 0;
00802     }
00803     benchmark::DoNotOptimize(cctz::load_time_zone(names[index], &tz));
00804   }
00805 }
00806 BENCHMARK(BM_Zone_LoadAllTimeZonesCached);
00807 
00808 void BM_Zone_TimeZoneEqualityImplicit(benchmark::State& state) {
00809   cctz::time_zone tz;  // implicit UTC
00810   while (state.KeepRunning()) {
00811     benchmark::DoNotOptimize(tz == tz);
00812   }
00813 }
00814 BENCHMARK(BM_Zone_TimeZoneEqualityImplicit);
00815 
00816 void BM_Zone_TimeZoneEqualityExplicit(benchmark::State& state) {
00817   cctz::time_zone tz = cctz::utc_time_zone();  // explicit UTC
00818   while (state.KeepRunning()) {
00819     benchmark::DoNotOptimize(tz == tz);
00820   }
00821 }
00822 BENCHMARK(BM_Zone_TimeZoneEqualityExplicit);
00823 
00824 void BM_Zone_UTCTimeZone(benchmark::State& state) {
00825   cctz::time_zone tz;
00826   while (state.KeepRunning()) {
00827     benchmark::DoNotOptimize(cctz::utc_time_zone());
00828   }
00829 }
00830 BENCHMARK(BM_Zone_UTCTimeZone);
00831 
00832 // In each "ToCivil" benchmark we switch between two instants separated
00833 // by at least one transition in order to defeat any internal caching of
00834 // previous results (e.g., see local_time_hint_).
00835 //
00836 // The "UTC" variants use UTC instead of the Google/local time zone.
00837 
00838 void BM_Time_ToCivil_CCTZ(benchmark::State& state) {
00839   const cctz::time_zone tz = TestTimeZone();
00840   std::chrono::system_clock::time_point tp =
00841       std::chrono::system_clock::from_time_t(1384569027);
00842   std::chrono::system_clock::time_point tp2 =
00843       std::chrono::system_clock::from_time_t(1418962578);
00844   while (state.KeepRunning()) {
00845     std::swap(tp, tp2);
00846     tp += std::chrono::seconds(1);
00847     benchmark::DoNotOptimize(cctz::convert(tp, tz));
00848   }
00849 }
00850 BENCHMARK(BM_Time_ToCivil_CCTZ);
00851 
00852 void BM_Time_ToCivil_Libc(benchmark::State& state) {
00853   // No timezone support, so just use localtime.
00854   time_t t = 1384569027;
00855   time_t t2 = 1418962578;
00856   struct tm tm;
00857   while (state.KeepRunning()) {
00858     std::swap(t, t2);
00859     t += 1;
00860 #if defined(_WIN32) || defined(_WIN64)
00861     benchmark::DoNotOptimize(localtime_s(&tm, &t));
00862 #else
00863     benchmark::DoNotOptimize(localtime_r(&t, &tm));
00864 #endif
00865   }
00866 }
00867 BENCHMARK(BM_Time_ToCivil_Libc);
00868 
00869 void BM_Time_ToCivilUTC_CCTZ(benchmark::State& state) {
00870   const cctz::time_zone tz = cctz::utc_time_zone();
00871   std::chrono::system_clock::time_point tp =
00872       std::chrono::system_clock::from_time_t(1384569027);
00873   while (state.KeepRunning()) {
00874     tp += std::chrono::seconds(1);
00875     benchmark::DoNotOptimize(cctz::convert(tp, tz));
00876   }
00877 }
00878 BENCHMARK(BM_Time_ToCivilUTC_CCTZ);
00879 
00880 void BM_Time_ToCivilUTC_Libc(benchmark::State& state) {
00881   time_t t = 1384569027;
00882   struct tm tm;
00883   while (state.KeepRunning()) {
00884     t += 1;
00885 #if defined(_WIN32) || defined(_WIN64)
00886     benchmark::DoNotOptimize(gmtime_s(&tm, &t));
00887 #else
00888     benchmark::DoNotOptimize(gmtime_r(&t, &tm));
00889 #endif
00890   }
00891 }
00892 BENCHMARK(BM_Time_ToCivilUTC_Libc);
00893 
00894 // In each "FromCivil" benchmark we switch between two YMDhms values
00895 // separated by at least one transition in order to defeat any internal
00896 // caching of previous results (e.g., see time_local_hint_).
00897 //
00898 // The "UTC" variants use UTC instead of the Google/local time zone.
00899 // The "Day0" variants require normalization of the day of month.
00900 
00901 void BM_Time_FromCivil_CCTZ(benchmark::State& state) {
00902   const cctz::time_zone tz = TestTimeZone();
00903   int i = 0;
00904   while (state.KeepRunning()) {
00905     if ((i++ & 1) == 0) {
00906       benchmark::DoNotOptimize(
00907           cctz::convert(cctz::civil_second(2014, 12, 18, 20, 16, 18), tz));
00908     } else {
00909       benchmark::DoNotOptimize(
00910           cctz::convert(cctz::civil_second(2013, 11, 15, 18, 30, 27), tz));
00911     }
00912   }
00913 }
00914 BENCHMARK(BM_Time_FromCivil_CCTZ);
00915 
00916 void BM_Time_FromCivil_Libc(benchmark::State& state) {
00917   // No timezone support, so just use localtime.
00918   int i = 0;
00919   while (state.KeepRunning()) {
00920     struct tm tm;
00921     if ((i++ & 1) == 0) {
00922       tm.tm_year = 2014 - 1900;
00923       tm.tm_mon = 12 - 1;
00924       tm.tm_mday = 18;
00925       tm.tm_hour = 20;
00926       tm.tm_min = 16;
00927       tm.tm_sec = 18;
00928     } else {
00929       tm.tm_year = 2013 - 1900;
00930       tm.tm_mon = 11 - 1;
00931       tm.tm_mday = 15;
00932       tm.tm_hour = 18;
00933       tm.tm_min = 30;
00934       tm.tm_sec = 27;
00935     }
00936     tm.tm_isdst = -1;
00937     benchmark::DoNotOptimize(mktime(&tm));
00938   }
00939 }
00940 BENCHMARK(BM_Time_FromCivil_Libc);
00941 
00942 void BM_Time_FromCivilUTC_CCTZ(benchmark::State& state) {
00943   const cctz::time_zone tz = cctz::utc_time_zone();
00944   while (state.KeepRunning()) {
00945     benchmark::DoNotOptimize(
00946         cctz::convert(cctz::civil_second(2014, 12, 18, 20, 16, 18), tz));
00947   }
00948 }
00949 BENCHMARK(BM_Time_FromCivilUTC_CCTZ);
00950 
00951 // There is no BM_Time_FromCivilUTC_Libc.
00952 
00953 void BM_Time_FromCivilDay0_CCTZ(benchmark::State& state) {
00954   const cctz::time_zone tz = TestTimeZone();
00955   int i = 0;
00956   while (state.KeepRunning()) {
00957     if ((i++ & 1) == 0) {
00958       benchmark::DoNotOptimize(
00959           cctz::convert(cctz::civil_second(2014, 12, 0, 20, 16, 18), tz));
00960     } else {
00961       benchmark::DoNotOptimize(
00962           cctz::convert(cctz::civil_second(2013, 11, 0, 18, 30, 27), tz));
00963     }
00964   }
00965 }
00966 BENCHMARK(BM_Time_FromCivilDay0_CCTZ);
00967 
00968 void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
00969   // No timezone support, so just use localtime.
00970   int i = 0;
00971   while (state.KeepRunning()) {
00972     struct tm tm;
00973     if ((i++ & 1) == 0) {
00974       tm.tm_year = 2014 - 1900;
00975       tm.tm_mon = 12 - 1;
00976       tm.tm_mday = 0;
00977       tm.tm_hour = 20;
00978       tm.tm_min = 16;
00979       tm.tm_sec = 18;
00980     } else {
00981       tm.tm_year = 2013 - 1900;
00982       tm.tm_mon = 11 - 1;
00983       tm.tm_mday = 0;
00984       tm.tm_hour = 18;
00985       tm.tm_min = 30;
00986       tm.tm_sec = 27;
00987     }
00988     tm.tm_isdst = -1;
00989     benchmark::DoNotOptimize(mktime(&tm));
00990   }
00991 }
00992 BENCHMARK(BM_Time_FromCivilDay0_Libc);
00993 
00994 const char* const kFormats[] = {
00995     RFC1123_full,         // 0
00996     RFC1123_no_wday,      // 1
00997     RFC3339_full,         // 2
00998     RFC3339_sec,          // 3
00999     "%Y-%m-%dT%H:%M:%S",  // 4
01000     "%Y-%m-%d",           // 5
01001 };
01002 const int kNumFormats = sizeof(kFormats) / sizeof(kFormats[0]);
01003 
01004 void BM_Format_FormatTime(benchmark::State& state) {
01005   const std::string fmt = kFormats[state.range(0)];
01006   state.SetLabel(fmt);
01007   const cctz::time_zone tz = TestTimeZone();
01008   const std::chrono::system_clock::time_point tp =
01009       cctz::convert(cctz::civil_second(1977, 6, 28, 9, 8, 7), tz) +
01010       std::chrono::microseconds(1);
01011   while (state.KeepRunning()) {
01012     benchmark::DoNotOptimize(cctz::format(fmt, tp, tz));
01013   }
01014 }
01015 BENCHMARK(BM_Format_FormatTime)->DenseRange(0, kNumFormats - 1);
01016 
01017 void BM_Format_ParseTime(benchmark::State& state) {
01018   const std::string fmt = kFormats[state.range(0)];
01019   state.SetLabel(fmt);
01020   const cctz::time_zone tz = TestTimeZone();
01021   std::chrono::system_clock::time_point tp =
01022       cctz::convert(cctz::civil_second(1977, 6, 28, 9, 8, 7), tz) +
01023       std::chrono::microseconds(1);
01024   const std::string when = cctz::format(fmt, tp, tz);
01025   while (state.KeepRunning()) {
01026     benchmark::DoNotOptimize(cctz::parse(fmt, when, tz, &tp));
01027   }
01028 }
01029 BENCHMARK(BM_Format_ParseTime)->DenseRange(0, kNumFormats - 1);
01030 
01031 }  // namespace


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