(背景:为什么我应该在C#中使用int而不是字节或short)
为了满足我自己对使用"适当大小"整数与"优化"整数的优缺点的好奇心,我编写了以下代码,强化了我以前在.Net中对int性能的真实性(并在链接中对此进行了解释)以上)它是针对int性能而不是short或byte进行优化的.
DateTime t;
long a, b, c;
t = DateTime.Now;
for (int index = 0; index < 127; index++)
{
Console.WriteLine(index.ToString());
}
a = DateTime.Now.Ticks - t.Ticks;
t = DateTime.Now;
for (short index = 0; index < 127; index++)
{
Console.WriteLine(index.ToString());
}
b=DateTime.Now.Ticks - t.Ticks;
t = DateTime.Now;
for (byte index = 0; index < 127; index++)
{
Console.WriteLine(index.ToString());
}
c=DateTime.Now.Ticks - t.Ticks;
Console.WriteLine(a.ToString());
Console.WriteLine(b.ToString());
Console.WriteLine(c.ToString());
Run Code Online (Sandbox Code Playgroud)
这在......的范围内给出了大致一致的结果.
〜95万
〜2000000
〜1700000
这与我期望看到的一致.
但是当我尝试重复每个数据类型的循环时......
t = DateTime.Now;
for (int …Run Code Online (Sandbox Code Playgroud)