Int32 vs Int64 performance on a 64 bit platform?
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;
开发者_StackOverflow中文版 }
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.
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.
There's a similar question on this here: Will using longs instead of ints benefit in 64bit java
Typically, in a real application you're more concerned about cache misses, so this is less of a concern overall.
精彩评论