Is it a good practice to use byte/short as counter variable when we know the exact number of loops? for example
for (byte i = 1; i <= 26; i++)
vs
for (short i = 1; i <= 26; i++)
vs
for (int i = 1; i <=26; i++)
Is it a good practice to use byte/short as counter variable when we know the exact number of loops? for example
for (byte i = 1; i <= 26; i++)
vs
for (short i = 1; i <= 26; i++)
vs
for (int i = 1; i <=26; i++)
It is more likely to be confusing than helpful. Most developers expect to see an int
value and you only have 32-bit or 64-bit registers in your CPU so it won't change how your program works or performs.
There are many options which work and are not harmful to your program but you need to think about the poor developer who has to read it and understand it later, this could be you 6 months from now. ;)
It is also not worth making such a change even if the performance were faster unless it was dramatically faster. Consider this change.
for (byte i = 1; i <= 120; i++)
or
for (byte i = 1; i <= x; i++)
You might thing this is fine as 200 < 2^8 and it compiles just fine, but it's actually an infinite loop.
You have to ask the question; How much faster does it have to be, if you increase the risk of introducing a bug later?
Usually the answer is it has to make my whole program significantly faster in a way I have measured (not just the bit you change) AND I have to need it to be significantly faster.
Short answer: No.
Long answer: No, because CPU's are optimized for integer operations. If you work with bytes or shorts, the CPU constantly has to convert it to integers and back, generally by applying bitmasks.
When you perform some operations on short or byte variable you have to explicitly typecast it back to the required type in java. So it is preferred to use int in place of byte and short. Example :
short s = 0;
s= (short) (s+10);
If you do not typecast it to int it will throw compile time error : Type mismatch: cannot convert from int to short
So it is preferred to use int.
No it is not.
byte and short variables are 32bit/64bit size at this point and the VM is optimized for int.
"Premature optimization is the root of all evil"
If range of data type not exceed in your scenario. There is no issue in both cases.
But int
is preferred.
It also takes a row of memory which usually is 4 or 8 bytes. This means there will be no optumization for memory usage too.
Also If your system uses fragmentation algos for deviding rows of memory to partial addressing of these values, Its adressing algorithm will cost much!
As a starting point, use int
which is probably native to the architecture and you'll avoid possible implicit conversions when comparing i
with other bits and pieces. So my bet is that both byte
and short
will wind up being slower.
A far cuter optimisation would be to use ++i
rather than i++
, as the former will never be slower than the latter. (Conceptually i++
has to return a copy even though it will be optimised out by a good java compiler).