when to use smaller integers like byte or short rather than INTEGER

  • Thread starter Thread starter David
  • Start date Start date
D

David

If I have a loop that has a max value of say 20 why would I not want to
define the loop counter using a BYTE or SHORT as opposed to a INTEGER.

It seems in most books or samples they define fairly small numbers as
INTEGER.

thanks, David
 
If I have a loop that has a max value of say 20 why would I not want to
define the loop counter using a BYTE or SHORT as opposed to a INTEGER.

It seems in most books or samples they define fairly small numbers as
INTEGER.

thanks, David

You know it probably doesn't really matter - but, at least in the old
days using a 32-bit loop counter would give you a slightly better
performing loop :) Simply because it was the native word size of the
processor.
 
David said:
If I have a loop that has a max value of say 20 why would I not want to
define the loop counter using a BYTE or SHORT as opposed to a INTEGER.

It seems in most books or samples they define fairly small numbers as
INTEGER.

'Integer' is IMO the natural choice. Note that bounds may change over time.
The range of 'Integer' is large enough for common scenarios. In addition,
'Integer' as a 32-bit data type is very suitable for 32-bit computers. Most
classes in the .NET Framework use 'Integer' for indices. Using other types
may require (implicit widening) type conversions.

However, 'Byte and 'Short' are not entirely useless. They are often
required in p/invoke scenarios and when reading binary data from streams
(files, network streams, etc.). Arrays of 'Byte' are typically used to
store raw binary data.
 
David,

The smaller numbers are in my idea only suitable as they are used in old
databases or DLL's.

Be aware that in a 64bit computer the Int64 is probably the best. In past
the Integer was the type that was most suitable to the in those days most
used processor. A strong loby from old Microsoft Developing tool users (in
my idea VB6) has changed that and fixed it to 32Bits for the future.

This means that in fact the Integer will become in future proabably
everytime more outdated now that it is fixed to the 32Bit computer. (It is
not very bad, it uses only (ProcessorBits/IntBits * cycles) instead of 1 and
mostly has a new computer enough cycles to hide this for you).

Cor
 
Back
Top