I have about 70-150 different structs X with an unsigned integral ID field. They are read in and initialized on initialization of program and never modified thereafter. What would be the fastest way to access them (which happens a lot) among the following (or some other method?):
-
Use a std::vector v; where v[X.id] = X; to access by doing X& x = v[id]; (this should do a copy at the beginning but later on merely do a lookup by id on essentially a flat array.
-
Same as above but std::vector v; with X* x = v[id]; I am wary about this one because it has one extra level of indirection.
-
a std::map – feels like overkill compared to above?
-
same as above but unordered_map – again given 70-150 occurrences might not even beat suggestion 3.
-
Anything more clever? One problem I see with 1 is it might be a bit sparse in access patterns but not sure how to address that if that’s the fastest way.
Using vector will be definitely the fastest approach: