My question is relatively simply. On 32bit platforms it's best to use Int32 as opposed to short or long due to to the cpu processing 32 bits at a time. So on a 64 bit architecture does this mean it's faster to use longs for performance? I created a quick and dirty app that copies int and long arrays to test the benchmarks. Here is the code(I did warn it's dirty):
static void Main(string[] args)
{
var lar = new long[256];
for(int z = 1; z<=256;z++)
{
lar[z-1] = z;
}
var watch = DateTime.Now;
for (int z = 0; z < 100000000; z++)
{
var lard = new long[256];
lar.CopyTo(lard, 0);
}
var res2 = watch - DateTime.Now;
var iar = new int[256];
for (int z = 1; z <= 256; z++)
{
iar[z - 1] = z;
}
watch = DateTime.Now;
for (int z = 0; z < 100000000; z++)
{
var iard = new int[256];
iar.CopyTo(iar, 0);
}
var res1 = watch - DateTime.Now;
Console.WriteLine(res1);
Console.WriteLine(res2);
}
The results it produces make long about 3 times as fast as int. Which makes me curious to whether I should start using longs for counters and such. I also did a similar counter test and long was insignificantly faster. Does anybody have any input on this? I also understand even if longs are faster they will still take up twice as much space.
Int32 will be equally as efficient on all x86, x64, and IA64. On an x64 and on an IA64 both Int32 and Int64 are equally as efficient. On an x86 Int32 will be more efficient than an Int64.
Int32 is used to represents 32-bit signed integers . Int64 is used to represents 64-bit signed integers.
In C#, Int64 Struct is used to represent 64-bit signed integer(also termed as long data type) starting from range -9,223,372,036,854,775,808 to +9, 223,372,036,854,775,807.
Remarks. Int64 is an immutable value type that represents signed integers with values that range from negative 9,223,372,036,854,775,808 (which is represented by the Int64. MinValue constant) through positive 9,223,372,036,854,775,807 (which is represented by the Int64.
No. It takes longer to do 64-bit on a 32-bit CPU because the CPU can only handle 32-bits at a time. A 64-bit CPU will just ignore the missing 32-bits.
Also, try not to preemptively optimize too much. Only optimize when there is a noticeable bottleneck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With