以下简短但完整的示例程序
const long iterations = 1000000000; T[] array = new T[1 << 20]; for (int i = 0; i < array.Length; i++) { array[i] = new T(); } Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < iterations; i++) { array[i % array.Length].Value0 = i; } Console.WriteLine("{0,-15} {1} {2:n0} iterations/s",typeof(T).Name,sw.Elapsed,iterations * 1000d / sw.ElapsedMilliseconds);
T替换为以下类型
class SimpleClass struct SimpleStruct { { public int Value0; public int Value0; } } class ComplexClass struct ComplexStruct { { public int Value0; public int Value0; public int Value1; public int Value1; public int Value2; public int Value2; public int Value3; public int Value3; public int Value4; public int Value4; public int Value5; public int Value5; public int Value6; public int Value6; public int Value7; public int Value7; public int Value8; public int Value8; public int Value9; public int Value9; public int Value10; public int Value10; public int Value11; public int Value11; } }
在我的机器上产生以下有趣的结果(Windows 7 .NET 4.5 32位)
SimpleClass 00:00:10.4471717 95,721,260 iterations/s ComplexClass 00:00:37.8199150 26,441,736 iterations/s SimpleStruct 00:00:12.3075100 81,254,571 iterations/s ComplexStruct 00:00:32.6140182 30,661,679 iterations/s
问题1:为什么ComplexClass比SimpleClass慢得多?经过的时间似乎随着课堂中的字段数呈线性增加.写入具有很多字段的类的第一个字段不应该与写入只有一个字段的类的第一个字段非常不同吗?
问题2:为什么ComplexStruct比SimpleStruct慢?看看IL代码显示我是直接写入数组,而不是复制到本地的ComplexStruct实例,然后复制到数组中.所以应该没有由复制更多的字段引起的开销.
奖金问题:为什么ComplexStruct比ComplexClass快?
编辑:用更小的数组T [] array = new T [1 < 8] ;:
SimpleClass 00:00:13.5091446 74,024,724 iterations/s ComplexClass 00:00:13.2505217 75,471,698 iterations/s SimpleStruct 00:00:14.8397693 67,389,986 iterations/s ComplexStruct 00:00:13.4821834 74,172,971 iterations/s
所以SimpleClass和ComplexClass几乎没有区别,只有SimpleStruct和ComplexStruct之间的区别很小.然而,SimpleClass和SimpleStruct的性能显着降低.
编辑:现在用T [] array = new T [1< 16] ;:
SimpleClass 00:00:09.7477715 102,595,670 iterations/s ComplexClass 00:00:10.1279081 98,745,927 iterations/s SimpleStruct 00:00:12.1539631 82,284,210 iterations/s ComplexStruct 00:00:10.5914174 94,419,790 iterations/s
1 <<15的结果为1 <8,1 <17的结果为1 <20.