For my embedded project I was using an unordered_map with hex values as keys and uint32_t as values. However they were very costly and some benchmarking revealed that using vectors instead were much much faster. (10us vs. 1us during an interrupt which lasts only 50us)
Since the keys are only meant for ordering the values and for readability I did not see any benefit, other than ease of use, over the unordered_map.
However, I would like to know if doing something like this is reliable. In the sense that will I lose data and/or ordering after, for example, insert operations to merge 2 vectors together? My vector looks like so: std::vector<uint32_t> and the indeces are always different
Another downside of using vectors is that I have to pre-initialize the vector with the required positions which constrains the flexibility of this method.
// The PARAM_ values are indeces in HEX
dataMapA.insert(dataMapA.begin() + PARAM_MOTOR_ANGLE, 0);
dataMapA.insert(dataMapA.begin() + PARAM_MOTOR_VELOCITY, 0);
dataMapA.insert(dataMapA.begin() + PARAM_JOINT_ANGLE, 0);
dataMapA.insert(dataMapA.begin() + PARAM_SPRING_ANGLE, 0);
If I don't do the above, I can not store any values.
The following could have been a solution but doesn't seem to work when defined as a private/public member. Eclipse throws a syntax error.
std::vector<uint32_t> dataMapA(10);
So I guess this question can be generalized in how to use vectors as a key-value lookup table and have it be as efficient as possible while still maintaining some sense of readability.
Aucun commentaire:
Enregistrer un commentaire