I have a data set and i have the choice between 2 structures. I have a
computation formula which needs to access to element of data.
On the first hand, i have a direct access to element (but a big big
structure) and in the formula i make useless operations (addition,
multiplication).
On the other hand, i must make a test (==) to access to the good element
(but a smaller structure) and in the formula i don't make any useless
operations.
I just want to know if a test isn't too costly in order to choose the
smaller structure
No general answer is possible. The C++ language defines how various
things work. It does not specify relative speed or efficiency of
anything. There are processors, unlike the Pentium and others used in
Windows PCs, that can do integer multiplications in a single clock
cycle, just as fast as they can do an integer comparison. In fact
there are some that can do floating point multiplications in a single
clock cycle.
If you absolutely need to know which is faster on your platform
running code generated by your compiler, write some test programs
using both methods and do some timing tests.
Generally the best approach is to write your program using the method
that generates the clearest, most easily read and understood code.
Then, when your program works correctly and only then, if the
performance of the program is not good enough, use a tool like a
profile to find the bottle necks. If and only if this routine is a
significant bottle neck in a too-slow program, experiment with ways to
speed it up.
If you worry about things like this ahead of time, you often wind up
spending a lot of effort on something that only makes a 1/10th of 1%
difference in the speed of the final code.