Improve performance of importing phases

Replace the O(k) implementation of Phase::speciesIndex with one that is
O(log(k)). This function is called frequently as part of Kinetics object
initialization. Optimize for the more common case where the species name is not
qualified with a phase name.
This commit is contained in:
Ray Speth 2015-03-31 15:37:14 -04:00
parent bde0f1466d
commit d1cda15f42
2 changed files with 13 additions and 11 deletions

View file

@ -894,6 +894,9 @@ private:
//! Vector of the species names
std::vector<std::string> m_speciesNames;
//! Map of species names to indices
std::map<std::string, size_t> m_speciesIndices;
size_t m_mm; //!< Number of elements.
vector_fp m_atomicWeights; //!< element atomic weights (kg kmol-1)
vector_int m_atomicNumbers; //!< element atomic numbers

View file

@ -256,19 +256,17 @@ void Phase::getAtoms(size_t k, double* atomArray) const
size_t Phase::speciesIndex(const std::string& nameStr) const
{
std::string pn;
std::string sn = parseSpeciesName(nameStr, pn);
if (pn == "" || pn == m_name || pn == m_id) {
vector<string>::const_iterator it = m_speciesNames.begin();
for (size_t k = 0; k < m_kk; k++) {
if (*it == sn) {
return k;
}
++it;
if (nameStr.find(':') != npos) {
std::string pn;
std::string sn = parseSpeciesName(nameStr, pn);
if (pn == "" || pn == m_name || pn == m_id) {
return getValue(m_speciesIndices, sn, npos);
} else {
return npos;
}
return npos;
} else {
return getValue(m_speciesIndices, nameStr, npos);
}
return npos;
}
string Phase::speciesName(size_t k) const
@ -868,6 +866,7 @@ bool Phase::addSpecies(shared_ptr<Species>& spec) {
}
m_speciesNames.push_back(spec->name);
m_speciesIndices[spec->name] = m_kk;
m_speciesCharge.push_back(spec->charge);
m_speciesSize.push_back(spec->size);
size_t ne = nElements();