您的位置:首页 > 其它

值类型和引用类型在hashtable里面性能比较分析1 -GetHashCode()

2006-10-20 09:38 337 查看
上篇文章中,测试了一下值类型数据和引用类型数据在hashtable中插入和读取的性能,测试结果和本人预期也有一些出入,msdn有一篇文章介绍在box,unbox的时候,性能关系为:class>interface>int,原文:Open the Box! Quick,进一步分析了上篇测试,发现其实影响测试性能还有其它几个方面的因素,本篇就针对不同数据类型在GetHashCode()上面的消耗
测试程序如下;

1 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
2 stopWatch.Start();
3 for (int i = 0; i < CompareCount; i++)
4 stopWatch.Stop();
8 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
9 stopWatch2.Start();
10 for (int i = 0; i < CompareCount; i++)
11 stopWatch2.Stop();
15 System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();
16 stopWatch3.Start();
17 for (int i = 0; i < CompareCount; i++)
18 stopWatch3.Stop();
22 long t1 = stopWatch.ElapsedTicks;
23 long t2 = stopWatch2.ElapsedTicks;
24 long t3 = stopWatch3.ElapsedTicks;
25 Console.WriteLine(t1.ToString());
26 Console.WriteLine(t2.ToString());
27 Console.WriteLine(t3.ToString());
28 Console.WriteLine((double)(t2/t1));
29 ArrayList list = new ArrayList();
30 }

结果表,我暂且不画在这上面了,因为比较多,而且不好排版,我只说一下结果,int类型的数据比字符串类型的在GetHashCode()的效率上要高50多倍,比Class的也高50-70倍
察看了GetHashCode()的实现
Int32

1public override int GetHashCode()
2
6
String

1[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
2public override unsafe int GetHashCode()
3
24
25

而object的实现方法,我 不太理解:

1public virtual int GetHashCode()
2
6
7[MethodImpl(MethodImplOptions.InternalCall)]
8internal static extern int InternalGetHashCode(object obj);
9
object的具体处理方法
而从int,string类型算法可以轻易看出效率差别,虽然string类型还有unsafe .

而且,值类型必须重写GetHashCode。
从这里可以看出,在上次测试中,GetHashCode帮了int类型不少忙呀!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: