I know this is probably a stupid question, but I wanted to make sure and I couldn’t readily find this information.
What is the performance characteristic of find() in an unordered map? Is it as fast/nearly as fast as a normal lookup?
I.e.
std::string defaultrow = sprite.attribute("defaultrow").value();
auto rclassIt = Rows::NameRowMap.find(defaultrow);
if (rclassIt != Rows::NameRowMap.end())
defRow = rclassIt->second;
vs.
std::string defaultrow = sprite.attribute("defaultrow").value();
defRow = Rows::NameRowMap[defaultrow];
where Rows::NameRowMap is a unordered map mapping a string index to an int.
In my case, I don’t know for certain if the key will exist before hand, so the first solution seemed safer to me, but if I can guarantee existence, is the second case faster (ignoring the extra checks I’m doing)? and if so, why? If it matters, I’m using the 1.46 boost implementation
findandoperator[]on an unordered container are O(1) average, O(n) worst-case — it depends on the quality of your hash function.