Restructuring some code, and the way I built it up over time has portions that look something like this:
s.replace("ABW"," Aruba ");
s.replace("AFG"," Afghanistan ");
s.replace("AGO"," Angola ");
s.replace("AIA"," Anguilla ");
s.replace("ALA"," Åland Islands ");
s.replace("ALB"," Albania ");
s.replace("AND"," Andorra ");
s.replace("ARE"," United Arab Emirates ");
s.replace("ARG"," Argentina ");
s.replace("ARM"," Armenia ");
s.replace("ASM"," American Samoa ");
s.replace("ATA"," Antarctica ");
s.replace("ATF"," French Southern Territories ");
s.replace("ATG"," Antigua and Barbuda ");
s.replace("AUS"," Australia ");
s.replace("AUT"," Austria ");
s.replace("AZE"," Azerbaijan ");
s.replace("BDI"," Burundi ");
s.replace("BEL"," Belgium ");
s.replace("BEN"," Benin ");
s.replace("BES"," Bonaire, Sint Eustatius and Saba ");
s.replace("BFA"," Burkina Faso ");
s.replace("BGD"," Bangladesh ");
s.replace("BGR"," Bulgaria ");
s.replace("BHR"," Bahrain ");
s.replace("BHS"," Bahamas ");
s.replace("BIH"," Bosnia and Herzegovina ");
s.replace("BLM"," Saint Barthélemy ");
s.replace("BLR"," Belarus ");
s.replace("BLZ"," Belize ");
s.replace("BMU"," Bermuda ");
s.replace("BOL"," Plurinational State of Bolivia, ");
s.replace("BRA"," Brazil ");
s.replace("BRB"," Barbados ");
s.replace("BRN"," Brunei Darussalam ");
s.replace("BTN"," Bhutan ");
s.replace("BVT"," Bouvet Island ");
s.replace("BWA"," Botswana ");
s.replace("CAF"," Central African Republic ");
s.replace("CAN"," Canada ");
s.replace("CCK"," Cocos (Keeling) Islands ");
s.replace("CHE"," Switzerland ");
s.replace("CHL"," Chile ");
s.replace("CHN"," China ");
s.replace("CIV"," Côte d'Ivoire ");
s.replace("CMR"," Cameroon ");
s.replace("COD"," the Democratic Republic of the Congo ");
s.replace("COG"," Congo ");
s.replace("COK"," Cook Islands ");
s.replace("COL"," Colombia ");
s.replace("COM"," Comoros ");
s.replace("CPV"," Cabo Verde ");
s.replace("CRI"," Costa Rica ");
s.replace("CUB"," Cuba ");
s.replace("CUW"," Curaçao ");
s.replace("CXR"," Christmas Island ");
s.replace("CYM"," Cayman Islands ");
s.replace("CYP"," Cyprus ");
s.replace("CZE"," Czechia ");
s.replace("DEU"," Germany ");
s.replace("DJI"," Djibouti ");
s.replace("DMA"," Dominica ");
s.replace("DNK"," Denmark ");
s.replace("DOM"," Dominican Republic ");
s.replace("DZA"," Algeria ");
s.replace("ECU"," Ecuador ");
s.replace("EGY"," Egypt ");
s.replace("ERI"," Eritrea ");
s.replace("ESH"," Western Sahara ");
s.replace("ESP"," Spain ");
s.replace("EST"," Estonia ");
s.replace("ETH"," Ethiopia ");
s.replace("FIN"," Finland ");
s.replace("FJI"," Fiji ");
s.replace("FLK"," Falkland Islands (Malvinas) ");
s.replace("FRA"," France ");
s.replace("FRO"," Faroe Islands ");
s.replace("FSM"," Federated States of Micronesia, ");
s.replace("GAB"," Gabon ");
s.replace("GBR"," United Kingdom ");
s.replace("GEO"," Georgia ");
s.replace("GGY"," Guernsey ");
s.replace("GHA"," Ghana ");
s.replace("GIB"," Gibraltar ");
s.replace("GIN"," Guinea ");
s.replace("GLP"," Guadeloupe ");
s.replace("GMB"," Gambia ");
s.replace("GNB"," Guinea-Bissau ");
s.replace("GNQ"," Equatorial Guinea ");
s.replace("GRC"," Greece ");
s.replace("GRD"," Grenada ");
s.replace("GRL"," Greenland ");
s.replace("GTM"," Guatemala ");
s.replace("GUF"," French Guiana ");
s.replace("GUM"," Guam ");
s.replace("GUY"," Guyana ");
s.replace("HKG"," Hong Kong ");
s.replace("HMD"," Heard Island and McDonald Islands ");
s.replace("HND"," Honduras ");
s.replace("HRV"," Croatia ");
s.replace("HTI"," Haiti ");
s.replace("HUN"," Hungary ");
s.replace("IDN"," Indonesia ");
s.replace("IMN"," Isle of Man ");
s.replace("IND"," India ");
s.replace("IOT"," British Indian Ocean Territory ");
s.replace("IRL"," Ireland ");
s.replace("IRN"," Islamic Republic of Iran, ");
s.replace("IRQ"," Iraq ");
s.replace("ISL"," Iceland ");
s.replace("ISR"," Israel ");
s.replace("ITA"," Italy ");
s.replace("JAM"," Jamaica ");
s.replace("JEY"," Jersey ");
s.replace("JOR"," Jordan ");
s.replace("JPN"," Japan ");
s.replace("KAZ"," Kazakhstan ");
s.replace("KEN"," Kenya ");
s.replace("KGZ"," Kyrgyzstan ");
s.replace("KHM"," Cambodia ");
s.replace("KIR"," Kiribati ");
s.replace("KNA"," Saint Kitts and Nevis ");
s.replace("KOR"," Republic of Korea, ");
s.replace("KWT"," Kuwait ");
s.replace("LAO"," People's Democratic Republic Lao ");
s.replace("LBN"," Lebanon ");
s.replace("LBR"," Liberia ");
s.replace("LBY"," Libya ");
s.replace("LCA"," Saint Lucia ");
s.replace("LIE"," Liechtenstein ");
s.replace("LKA"," Sri Lanka ");
s.replace("LSO"," Lesotho ");
s.replace("LTU"," Lithuania ");
s.replace("LUX"," Luxembourg ");
s.replace("LVA"," Latvia ");
s.replace("MAC"," Macao ");
s.replace("MAF"," Saint Martin (French part) ");
s.replace("MAR"," Morocco ");
s.replace("MCO"," Monaco ");
s.replace("MDA"," Moldova, Republic of ");
s.replace("MDG"," Madagascar ");
s.replace("MDV"," Maldives ");
s.replace("MEX"," Mexico ");
s.replace("MHL"," Marshall Islands ");
s.replace("MKD"," the former Yugoslav Republic of Macedonia, ");
s.replace("MLI"," Mali ");
s.replace("MLT"," Malta ");
s.replace("MMR"," Myanmar ");
s.replace("MNE"," Montenegro ");
s.replace("MNG"," Mongolia ");
s.replace("MNP"," Northern Mariana Islands ");
s.replace("MOZ"," Mozambique ");
s.replace("MRT"," Mauritania ");
s.replace("MSR"," Montserrat ");
s.replace("MTQ"," Martinique ");
s.replace("MUS"," Mauritius ");
s.replace("MWI"," Malawi ");
s.replace("MYS"," Malaysia ");
s.replace("MYT"," Mayotte ");
s.replace("NAM"," Namibia ");
s.replace("NCL"," New Caledonia ");
s.replace("NER"," Niger ");
s.replace("NFK"," Norfolk Island ");
s.replace("NGA"," Nigeria ");
s.replace("NIC"," Nicaragua ");
s.replace("NIU"," Niue ");
s.replace("NLD"," Netherlands ");
s.replace("NOR"," Norway ");
s.replace("NPL"," Nepal ");
s.replace("NRU"," Nauru ");
s.replace("NZL"," New Zealand ");
s.replace("OMN"," Oman ");
s.replace("PAK"," Pakistan ");
s.replace("PAN"," Panama ");
s.replace("PCN"," Pitcairn ");
s.replace("PER"," Peru ");
s.replace("PHL"," Philippines ");
s.replace("PLW"," Palau ");
s.replace("PNG"," Papua New Guinea ");
s.replace("POL"," Poland ");
s.replace("PRI"," Puerto Rico ");
s.replace("PRK"," Democratic People's Republic of Korea, ");
s.replace("PRT"," Portugal ");
s.replace("PRY"," Paraguay ");
s.replace("PSE"," State of Palestine, ");
s.replace("PYF"," French Polynesia ");
s.replace("QAT"," Qatar ");
s.replace("REU"," Réunion ");
s.replace("ROU"," Romania ");
s.replace("RUS"," Russian Federation ");
s.replace("RWA"," Rwanda ");
s.replace("SAU"," Saudi Arabia ");
s.replace("SDN"," Sudan ");
s.replace("SEN"," Senegal ");
s.replace("SGP"," Singapore ");
s.replace("SGS"," South Georgia and the South Sandwich Islands ");
s.replace("SHN"," Saint Helena, Ascension and Tristan da Cunha ");
s.replace("SJM"," Svalbard and Jan Mayen ");
s.replace("SLB"," Solomon Islands ");
s.replace("SLE"," Sierra Leone ");
s.replace("SLV"," El Salvador ");
s.replace("SMR"," San Marino ");
s.replace("SOM"," Somalia ");
s.replace("SPM"," Saint Pierre and Miquelon ");
s.replace("SRB"," Serbia ");
s.replace("SSD"," South Sudan ");
s.replace("STP"," Sao Tome and Principe ");
s.replace("SUR"," Suriname ");
s.replace("SVK"," Slovakia ");
s.replace("SVN"," Slovenia ");
s.replace("SWE"," Sweden ");
s.replace("SWZ"," Swaziland ");
s.replace("SXM"," Sint Maarten (Dutch part) ");
s.replace("SYC"," Seychelles ");
s.replace("SYR"," Syrian Arab Republic ");
s.replace("TCA"," Turks and Caicos Islands ");
s.replace("TCD"," Chad ");
s.replace("TGO"," Togo ");
s.replace("THA"," Thailand ");
s.replace("TJK"," Tajikistan ");
s.replace("TKL"," Tokelau ");
s.replace("TKM"," Turkmenistan ");
s.replace("TLS"," Timor-Leste ");
s.replace("TON"," Tonga ");
s.replace("TTO"," Trinidad and Tobago ");
s.replace("TUN"," Tunisia ");
s.replace("TUR"," Turkey ");
s.replace("TUV"," Tuvalu ");
s.replace("TWN"," Taiwan Province of China ");
s.replace("TZA"," United Republic of Tanzania, ");
s.replace("UGA"," Uganda ");
s.replace("UKR"," Ukraine ");
s.replace("UMI"," United States Minor Outlying Islands ");
s.replace("URY"," Uruguay ");
s.replace("USA"," United States of America ");
s.replace("UZB"," Uzbekistan ");
s.replace("VAT"," Holy See ");
s.replace("VCT"," Saint Vincent and the Grenadines ");
s.replace("VEN"," Bolivarian Republic of Venezuela, ");
s.replace("VGB"," British Virgin Islands, ");
s.replace("VIR"," U.S. Virgin Islands, ");
s.replace("VNM"," Viet Nam ");
s.replace("VUT"," Vanuatu ");
s.replace("WLF"," Wallis and Futuna ");
s.replace("WSM"," Samoa ");
s.replace("YEM"," Yemen ");
s.replace("ZAF"," South Africa ");
s.replace("ZMB"," Zambia ");
s.replace("ZWE"," Zimbabwe ");
That alone is about 250 calls to the QString::replace
function, which does not seem optimal. I have about 5000 similar types of calls, and my strings are very large, and I would like to learn the optimal way to tackle this issue.
I program in Qt, and just thinking off the top of my head, the most optimal way I could think of tackling this was
- Building a hash, with hardcoded
Key["ABW"] Value[" Aruba "]
pairs. - Building a regular expression
(ABW|AFG|AGO|...|ZMB)
- Matching the regular expression against the string
- Getting a list of matches
- Match that list against the hash somehow, and running the requisite replace calls.
I figure while on a better track than the 250 replace functions, I do not feel confident enough to ascertain whether it is the best track, or whether it really is even necessary, because for all I know, GCC and other compilers optimizes things like this on its own.
What is the best and most optimal strategy when it comes to tackling the issue of a massive number of replace
calls against a large string in terms of achieving the best performance?
Is there an industry standard when it comes to this sort of thing?
but is it actually a significant performance bottleneck?
-- No; I am happy with my speed. At most, I might do 10000 strings in a day, which probably averages about 10 seconds per iteration, and replacing probably comprises no more than 50% of that. I am surprised my code runs as fast as it does, However it would be nice if I could cut a few hours off my processing time.