To maniero’s response already gives an excellent overview, I would just like to complement the following part:
The most common if you have a very large volume of data that needs to be manipulated intensively is that you have some gain by the ability to cache more elements using a short than a int.
Generally speaking, the influence of a good cache on a system’s performance is something not to be underestimated. Often a lot of attention is paid to local performance (cast) and the overall performance is forgotten (in a miss cache, several cycles are wasted - more than the overhead an extra instruction or two, depending on the case).
None of this contradicts the answer mentioned: only if the volume of data is large is there an advantage (although perhaps I disagree on what would be "very" large). Also, it is quite different for you to have, for example, an array of objects:
class MeuObjeto {
Foo foo;
Bar bar;
Baz baz;
short s;
}
MeuObjeto[] array = new MeuObjeto[10000];
Or an array of shorts:
short[] array = new short[10000];
In the first case, the space saving is minimal - even if the objects are in contiguous memory positions (depending on the case, they may not be) - and that is if the memory alignment does not eliminate this space difference. The use of short instead of int will not have a significant impact on Misses cache, so that the overhead of the cast operations shall not receive any positive consideration.
In the second case, the story is different: it will take twice as long for you to have one miss cache if you are accessing these elements sequentially, compared to an array of int. Even for random hits, the chance of the data you want to already be in the cache is twice as high. So, even though each individual operation is a little less efficient, the cycles you "save" by avoiding the Misses can compensate - making complete operation faster.
(In any case, it remains worth the advice to avoid premature optimizations / micro optimizations)
I don’t like shorts. I prefer shorts anyway. #Piadanerddoano
– Marcelo Bezerra bovino