Improve performance of reading species info from XML

Avoid an O(N) lookup for the thermo info for each species. Using an
unordered_map would be even better, but there's no portable way to do that
without Boost.
This commit is contained in:
Ray Speth 2013-06-28 21:24:42 +00:00
parent 0a2db03364
commit ee9b153efb

View file

@ -353,6 +353,11 @@ static void formSpeciesXMLNodeList(std::vector<XML_Node*> &spDataNodeList,
} }
} }
} else { } else {
std::map<std::string, XML_Node*> speciesNodes;
for (size_t k = 0; k < db->nChildren(); k++) {
XML_Node& child = db->child(k);
speciesNodes[child["name"]] = &child;
}
for (size_t k = 0; k < nsp; k++) { for (size_t k = 0; k < nsp; k++) {
string stemp = spnames[k]; string stemp = spnames[k];
skip = false; skip = false;
@ -367,11 +372,12 @@ static void formSpeciesXMLNodeList(std::vector<XML_Node*> &spDataNodeList,
if (!skip) { if (!skip) {
declared[stemp] = true; declared[stemp] = true;
// Find the species in the database by name. // Find the species in the database by name.
XML_Node* s = db->findByAttr("name", stemp); std::map<std::string, XML_Node*>::iterator iter = speciesNodes.find(stemp);
if (!s) { if (iter == speciesNodes.end()) {
throw CanteraError("importPhase","no data for species, \"" throw CanteraError("importPhase","no data for species, \""
+ stemp + "\""); + stemp + "\"");
} }
XML_Node* s = iter->second;
nSpecies++; nSpecies++;
spNamesList.resize(nSpecies); spNamesList.resize(nSpecies);
spDataNodeList.resize(nSpecies, 0); spDataNodeList.resize(nSpecies, 0);