I’m dealing with a problem which needs to work with a lot of data. Currently its values are represented as an unsigned int. I know that real values do not exceed a limit of 1000.
Questions
-
I can use
unsigned shortto store it. An upside to this is that it’ll use less storage space to store the value. Will performance suffer? -
If I decided to store data as
shortbut all the calling functions useint, it’s recognized that I need to convert between these datatypes when storing or extracting values. Will performance suffer? Will the loss in performance be dramatic? -
If I decided to not use
shortbut just 10 bits packed into an array ofunsigned int. What will happen in this case comparing with previous ones?
This all depends on architecture. Bit-fields are generally slower, but if you are able to to significantly cut down memory usage with them, you can even gain in performance due to better CPU caching and similar things. Likewise with short (though it is not dramatic in any case).
The best way is to make your source code able to switch representation easily (at compilation time, of course). Then you will be able to test and profile different implementations in your specific circumstances just by, say, changing one
#define.Also, don’t forget about premature optimization rule. Make it work first. If it turns out to be slow/not fast enough, only then try to speed up.