[转]比较高效地实现从两个不同数组中提取相同部分组成新的数组(只支持Int类型) [C#]
上篇文章中,测试了一下值类型数据和引用类型数据在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();
 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
2 stopWatch.Start();
            stopWatch.Start();
3 for (int i = 0; i < CompareCount; i++)
            for (int i = 0; i < CompareCount; i++)
4 {
            {
5 int hashCode = i.GetHashCode();
                int hashCode = i.GetHashCode();
6 }
            }
7 stopWatch.Stop();
            stopWatch.Stop();
8 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
9 stopWatch2.Start();
            stopWatch2.Start();
10 for (int i = 0; i < CompareCount; i++)
            for (int i = 0; i < CompareCount; i++)
11 {
            {
12 int hashCode = i.ToString().GetHashCode();
                int hashCode = i.ToString().GetHashCode();
13 }
            }
14 stopWatch2.Stop();
            stopWatch2.Stop();
15 System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();
16 stopWatch3.Start();
            stopWatch3.Start();
17 for (int i = 0; i < CompareCount; i++)
            for (int i = 0; i < CompareCount; i++)
18 {
            {
19 int hashCode = new Test().GetHashCode();
                int hashCode = new Test().GetHashCode();
20 }
            }
21 stopWatch3.Stop();
            stopWatch3.Stop();
22 long t1 = stopWatch.ElapsedTicks;
            long t1 = stopWatch.ElapsedTicks;
23 long t2 = stopWatch2.ElapsedTicks;
            long t2 = stopWatch2.ElapsedTicks;
24 long t3 = stopWatch3.ElapsedTicks;
            long t3 = stopWatch3.ElapsedTicks;
25 Console.WriteLine(t1.ToString());
            Console.WriteLine(t1.ToString());
26 Console.WriteLine(t2.ToString());
            Console.WriteLine(t2.ToString());
27 Console.WriteLine(t3.ToString());
            Console.WriteLine(t3.ToString());
28 Console.WriteLine((double)(t2/t1));
            Console.WriteLine((double)(t2/t1));
29 ArrayList list = new ArrayList();
            ArrayList list = new ArrayList();
30 }
        }
 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();2
 stopWatch.Start();
            stopWatch.Start();3
 for (int i = 0; i < CompareCount; i++)
            for (int i = 0; i < CompareCount; i++)4
 {
            {5
 int hashCode = i.GetHashCode();
                int hashCode = i.GetHashCode();6
 }
            }7
 stopWatch.Stop();
            stopWatch.Stop();8
 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();9
 stopWatch2.Start();
            stopWatch2.Start();10
 for (int i = 0; i < CompareCount; i++)
            for (int i = 0; i < CompareCount; i++)11
 {
            {12
 int hashCode = i.ToString().GetHashCode();
                int hashCode = i.ToString().GetHashCode();13
 }
            }14
 stopWatch2.Stop();
            stopWatch2.Stop();15
 System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();
            System.Diagnostics.Stopwatch stopWatch3 = new System.Diagnostics.Stopwatch();16
 stopWatch3.Start();
            stopWatch3.Start();17
 for (int i = 0; i < CompareCount; i++)
            for (int i = 0; i < CompareCount; i++)18
 {
            {19
 int hashCode = new Test().GetHashCode();
                int hashCode = new Test().GetHashCode();20
 }
            }21
 stopWatch3.Stop();
            stopWatch3.Stop();22
 long t1 = stopWatch.ElapsedTicks;
            long t1 = stopWatch.ElapsedTicks;23
 long t2 = stopWatch2.ElapsedTicks;
            long t2 = stopWatch2.ElapsedTicks;24
 long t3 = stopWatch3.ElapsedTicks;
            long t3 = stopWatch3.ElapsedTicks;25
 Console.WriteLine(t1.ToString());
            Console.WriteLine(t1.ToString());26
 Console.WriteLine(t2.ToString());
            Console.WriteLine(t2.ToString());27
 Console.WriteLine(t3.ToString());
            Console.WriteLine(t3.ToString());28
 Console.WriteLine((double)(t2/t1));
            Console.WriteLine((double)(t2/t1));29
 ArrayList list = new ArrayList();
            ArrayList list = new ArrayList();30
 }
        }结果表,我暂且不画在这上面了,因为比较多,而且不好排版,我只说一下结果,int类型的数据比字符串类型的在GetHashCode()的效率上要高50多倍,比Class的也高50-70倍
察看了GetHashCode()的实现
Int32
1 public override int GetHashCode()
public override int GetHashCode()
2 {
{
3 return this;
      return this;
4 }
}
5
6
 public override int GetHashCode()
public override int GetHashCode()2
 {
{3
 return this;
      return this;4
 }
}5

6

String
 1 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
2 public override unsafe int GetHashCode()
public override unsafe int GetHashCode()
3 {
{
4 fixed (char* text1 = ((char*) this))
      fixed (char* text1 = ((char*) this))
5 {
      {
6 char* chPtr1 = text1;
            char* chPtr1 = text1;
7 int num1 = 0x15051505;
            int num1 = 0x15051505;
8 int num2 = num1;
            int num2 = num1;
9 int* numPtr1 = (int*) chPtr1;
            int* numPtr1 = (int*) chPtr1;
10 for (int num3 = this.Length; num3 > 0; num3 -= 4)
            for (int num3 = this.Length; num3 > 0; num3 -= 4)
11 {
            {
12 num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
                  num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
13 if (num3 <= 2)
                  if (num3 <= 2)
14 {
                  {
15 break;
                        break;
16 }
                  }
17 num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
                  num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
18 numPtr1 += 2;
                  numPtr1 += 2;
19 }
            }
20 return (num1 + (num2 * 0x5d588b65));
            return (num1 + (num2 * 0x5d588b65));
21 }
      }
22 }
}
23
24 
 
25
 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]2
 public override unsafe int GetHashCode()
public override unsafe int GetHashCode()3
 {
{4
 fixed (char* text1 = ((char*) this))
      fixed (char* text1 = ((char*) this))5
 {
      {6
 char* chPtr1 = text1;
            char* chPtr1 = text1;7
 int num1 = 0x15051505;
            int num1 = 0x15051505;8
 int num2 = num1;
            int num2 = num1;9
 int* numPtr1 = (int*) chPtr1;
            int* numPtr1 = (int*) chPtr1;10
 for (int num3 = this.Length; num3 > 0; num3 -= 4)
            for (int num3 = this.Length; num3 > 0; num3 -= 4)11
 {
            {12
 num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];
                  num1 = (((num1 << 5) + num1) + (num1 >> 0x1b)) ^ numPtr1[0];13
 if (num3 <= 2)
                  if (num3 <= 2)14
 {
                  {15
 break;
                        break;16
 }
                  }17
 num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];
                  num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr1[1];18
 numPtr1 += 2;
                  numPtr1 += 2;19
 }
            }20
 return (num1 + (num2 * 0x5d588b65));
            return (num1 + (num2 * 0x5d588b65));21
 }
      }22
 }
}23

24
 
 25

而object的实现方法,我 不太理解:
1 public virtual int GetHashCode()
public virtual int GetHashCode()
2 {
{
3 return object.InternalGetHashCode(this);
      return object.InternalGetHashCode(this);
4 }
}
5
6 
 
7 [MethodImpl(MethodImplOptions.InternalCall)]
[MethodImpl(MethodImplOptions.InternalCall)]
8 internal static extern int InternalGetHashCode(object obj);
internal static extern int InternalGetHashCode(object obj);
9
 public virtual int GetHashCode()
public virtual int GetHashCode()2
 {
{3
 return object.InternalGetHashCode(this);
      return object.InternalGetHashCode(this);4
 }
}5

6
 
 7
 [MethodImpl(MethodImplOptions.InternalCall)]
[MethodImpl(MethodImplOptions.InternalCall)]8
 internal static extern int InternalGetHashCode(object obj);
internal static extern int InternalGetHashCode(object obj);9

 object的具体处理方法
而从int,string类型算法可以轻易看出效率差别,虽然string类型还有unsafe .
而且,值类型必须重写GetHashCode。
从这里可以看出,在上次测试中,GetHashCode帮了int类型不少忙呀! 
 
                    
                     
                    
                 
                    
                 
            
 
       
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号