I have a questionable coding practice.
When I need to iterate through a small list of items whose count limit is under 32000
, I use Int16
for my i variable type instead of Integer
. I do this because I assume using the Int16
is more efficient than a full blown Integer
.
Am I wrong? Is there no effective performance difference between using an Int16
vs an Integer
? Should I stop using Int16
and just stick with Integer
for all my counting/iteration needs?
Best Answer
You should almost always use
Int32
orInt64
(and, no, you do not get credit by usingUInt32
orUInt64
) when looping over an array or collection by index.The most obvious reason that it's less efficient is that all array and collection indexes found in the BCL take
Int32
s, so an implicit cast is always going to happen in code that tries to useInt16
s as an index.The less-obvious reason (and the reason that arrays take
Int32
as an index) is that the CIL specification says that all operation-stack values are eitherInt32
orInt64
. Every time you either load or store a value to any other integer type (Byte
,SByte
,UInt16
,Int16
,UInt32
, orUInt64
), there is an implicit conversion operation involved. Unsigned types have no penalty for loading, but for storing the value, this amounts to a truncation and a possible overflow check. For the signed types every load sign-extends, and every store sign-collapses (and has a possible overflow check).The place that this is going to hurt you most is the loop itself, not the array accesses. For example take this innocent-looking loop:
Looks good, right? Nope! You can basically ignore the initialization (
short i = 0
) since it only happens once, but the comparison (i<32000
) and incrementing (i++
) parts happen 32000 times. Here's some pesudo-code for what this thing looks like at the machine level:There are 3 conversions in there that are run 32000 times. And they could have been completely avoided by just using an
Int32
orInt64
.Update: As I said in the comment, I have now, in fact written a blog post on this topic, .NET Integral Data Types And You