views:

82

answers:

3

Possible Duplicate:
.NET Integer vs Int16?

Which is faster for large numbers of tests in a tight loop?

static bool IsEqual(Int16 a, Int16 b)
{
   return a==b;
}
static bool IsEqual(Int32 a, Int32 b)
{
   return a==b;
}
static bool IsEqual(Int64 a, Int64 b)
{
   return a==b;
}

or is there no difference?

+1  A: 

Possible duplicate of the link below. However this will help you -

http://stackoverflow.com/questions/129023/net-integer-vs-int16

Sachin Shanbhag
+2  A: 

Best thing to do is benchmark it and see for yourself.

Normally, on 32-bit architectures, there should either be no difference, or 32 should be faster (native word size is usually the fastest, even if smaller ints are supported). Ditto 64/64.

Nicholas Knight
+1  A: 

In theory the int16 is going to be slower than the int32 as it will be cast up to that before the comparison is made.

In practice, that's a small difference to begin with, and it'll quite likely be optimised away to a large extent. Therefore if something is naturally an int16 then don't use int32 just for the sake of this theoretical speed advantage. However, if it's just "naturally" integral, then use int32 over int16, as int32 is the "normal" integer size in .NET.

Jon Hanna