值类型和引用类型在hashtable里面性能比较分析1 -GetHashCode()
上篇文章中,测试了一下值类型数据和引用类型数据在hashtable中插入和读取的性能,测试结果和本人预期也有一些出入,msdn有一篇文章介绍在box,unbox的时候,性能关系为:class>interface>int,原文:Open the Box! Quick,进一步分析了上篇测试,发现其实影响测试性能还有其它几个方面的因素,本篇就针对不同数据类型在GetHashCode()上面的消耗
测试程序如下;
结果表,我暂且不画在这上面了,因为比较多,而且不好排版,我只说一下结果,int类型的数据比字符串类型的在GetHashCode()的效率上要高50多倍,比Class的也高50-70倍
察看了GetHashCode()的实现
Int32
而object的实现方法,我 不太理解:
测试程序如下;
1 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
2 stopWatch.Start();
3 for (int i = 0; i < CompareCount; i++)
4 {
5 int hashCode = i.GetHashCode();
6 }
7 stopWatch.Stop();
8 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
9 stopWatch2.Start();
10 for (int i = 0; i < CompareCount; i++)
11 {
12 int hashCode = i.ToString().GetHashCode();
13 }
14 stopWatch2.Stop();
15 System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();
16 stopWatch3.Start();
17 for (int i = 0; i < CompareCount; i++)
18 {
19 int hashCode = new Test().GetHashCode();
20 }
21 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 }
2 stopWatch.Start();
3 for (int i = 0; i < CompareCount; i++)
4 {
5 int hashCode = i.GetHashCode();
6 }
7 stopWatch.Stop();
8 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
9 stopWatch2.Start();
10 for (int i = 0; i < CompareCount; i++)
11 {
12 int hashCode = i.ToString().GetHashCode();
13 }
14 stopWatch2.Stop();
15 System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();
16 stopWatch3.Start();
17 for (int i = 0; i < CompareCount; i++)
18 {
19 int hashCode = new Test().GetHashCode();
20 }
21 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{
3 return this;
4}
5
6
2{
3 return this;
4}
5
6
String
1[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
2public override unsafe int GetHashCode()
3{
4 fixed (char* text1 = ((char*) this))
5 {
6 char* chPtr1 = text1;
7 int num1 = 0x15051505;
8 int num2 = num1;
9 int* numPtr1 = (int*) chPtr1;
10 for (int num3 = this.Length; num3 > 0; num3 -= 4)
11 {
12 num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
13 if (num3 <= 2)
14 {
15 break;
16 }
17 num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
18 numPtr1 += 2;
19 }
20 return (num1 + (num2 * 0x5d588b65));
21 }
22}
23
24
25
2public override unsafe int GetHashCode()
3{
4 fixed (char* text1 = ((char*) this))
5 {
6 char* chPtr1 = text1;
7 int num1 = 0x15051505;
8 int num2 = num1;
9 int* numPtr1 = (int*) chPtr1;
10 for (int num3 = this.Length; num3 > 0; num3 -= 4)
11 {
12 num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
13 if (num3 <= 2)
14 {
15 break;
16 }
17 num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
18 numPtr1 += 2;
19 }
20 return (num1 + (num2 * 0x5d588b65));
21 }
22}
23
24
25
而object的实现方法,我 不太理解:
1public virtual int GetHashCode()
2{
3 return object.InternalGetHashCode(this);
4}
5
6
7[MethodImpl(MethodImplOptions.InternalCall)]
8internal static extern int InternalGetHashCode(object obj);
9
2{
3 return object.InternalGetHashCode(this);
4}
5
6
7[MethodImpl(MethodImplOptions.InternalCall)]
8internal static extern int InternalGetHashCode(object obj);
9
object的具体处理方法
而从int,string类型算法可以轻易看出效率差别,虽然string类型还有unsafe .
而且,值类型必须重写GetHashCode。
从这里可以看出,在上次测试中,GetHashCode帮了int类型不少忙呀!
作者:jillzhang
出处:http://jillzhang.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://jillzhang.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。