Is it worth having a redundant "support container"?

122 views Asked by At

I have an std::vector (unordered), and on its specific elements (by their index) I have to call a method every cycle in an infinite loop.

But sometimes (maybe every 30.000th cycle) I want to search in them by the elements' member. (std::string)

Should I create an unordered_map to fasten up this search, or it doesn't worth the redundancy?

(The elements after adding them to the container, aren't removed, aren't moved, etc, they are only used.)

  • So when I add an element to the vector, I also add a pair<std::string, integer> to an unordered map.

    • This way when I search by its "name" I can get its index fast

    • Instead of slowly going through the vector and comparing strings.

    • (I think that unordered_map's at() is faster than lots of string comparing. Correct me If I'm wrong.)

0

There are 0 answers