值类型和引用类型在hashtable里面存取的性能比较

首先定义两个类:
 1 None.gif      public   interface  ITest
 2 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 3InBlock.gif        void M();
 4ExpandedBlockEnd.gif    }

 5 None.gif     public   class  Test1:ITest
 6 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 7InBlock.gif        public void M()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 9ExpandedSubBlockEnd.gif        }

10ExpandedBlockEnd.gif    }

11 None.gif    class  Test
12 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
13InBlock.gif            public Test()
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15ExpandedSubBlockEnd.gif            }

16ExpandedBlockEnd.gif        }
 首先,测试设置的速度hashtable.add()
 1 None.gif static   void  Main( string [] args)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {          
 3InBlock.gif            Hashtable table = new Hashtable();
 4InBlock.gif
 5InBlock.gif            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
 6InBlock.gif            stopWatch.Start();
 7InBlock.gif            for (int i = 0; i < CompareCount; i++)
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif                table.Add(i,new Test());
10ExpandedSubBlockEnd.gif            }

11InBlock.gif            stopWatch.Stop();
12InBlock.gif            
13InBlock.gif            for (int i = 0; i < CompareCount; i++)
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                Test o = table[i] as Test;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif           
18InBlock.gif            string t1 = stopWatch.ElapsedTicks.ToString();
19InBlock.gif          
20InBlock.gif            Hashtable table1 = new Hashtable();
21InBlock.gif            System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
22InBlock.gif            stopWatch1.Start();
23InBlock.gif            for (int i = 0; i < CompareCount; i++)
24ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
25InBlock.gif                table1.Add(i, i);
26ExpandedSubBlockEnd.gif            }

27InBlock.gif            stopWatch1.Stop();
28InBlock.gif        
29InBlock.gif            for (int i = 0; i < CompareCount; i++)
30ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
31InBlock.gif                int o = (int)table1[i];
32ExpandedSubBlockEnd.gif            }

33InBlock.gif           
34InBlock.gif            string t2 = stopWatch1.ElapsedTicks.ToString();
35InBlock.gif            Hashtable table2 = new Hashtable();
36InBlock.gif            System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
37InBlock.gif            stopWatch2.Start();
38InBlock.gif            for (int i = 0; i < CompareCount; i++)
39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
40InBlock.gif                ITest test2 = new Test1();
41InBlock.gif                table2.Add(i,test2);
42ExpandedSubBlockEnd.gif            }

43InBlock.gif
44InBlock.gif            stopWatch2.Stop();
45InBlock.gif            for (int i = 0; i < CompareCount; i++)
46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
47InBlock.gif                ITest o = table2[i] as ITest;
48ExpandedSubBlockEnd.gif            }

49InBlock.gif           
50InBlock.gif            string t3 = stopWatch2.ElapsedTicks.ToString();
51InBlock.gif            Console.WriteLine(t1);
52InBlock.gif            Console.WriteLine(t2);
53InBlock.gif            Console.WriteLine(t3);
54InBlock.gif            Console.WriteLine(((double)Convert.ToInt64(t1)/Convert.ToInt64(t2)).ToString());
55InBlock.gif            Console.WriteLine(((double)Convert.ToInt64(t3) / Convert.ToInt64(t2)).ToString());
56InBlock.gif            Console.Read();
57InBlock.gif            
58ExpandedBlockEnd.gif        }
测试获取的代码
 1 None.gif static   void  Main( string [] args)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {          
 3InBlock.gif            Hashtable table = new Hashtable();
 4InBlock.gif         
 5InBlock.gif            for (int i = 0; i < CompareCount; i++)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 7InBlock.gif                table.Add(i,new Test());
 8ExpandedSubBlockEnd.gif            }

 9InBlock.gif            
10InBlock.gif            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
11InBlock.gif            stopWatch.Start();
12InBlock.gif            for (int i = 0; i < CompareCount; i++)
13ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
14InBlock.gif                Test o = table[i] as Test;
15ExpandedSubBlockEnd.gif            }

16InBlock.gif            stopWatch.Stop();
17InBlock.gif            string t1 = stopWatch.ElapsedTicks.ToString();
18InBlock.gif          
19InBlock.gif            Hashtable table1 = new Hashtable();
20InBlock.gif           
21InBlock.gif            for (int i = 0; i < CompareCount; i++)
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                table1.Add(i, i);
24ExpandedSubBlockEnd.gif            }

25InBlock.gif            
26InBlock.gif            System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
27InBlock.gif            stopWatch1.Start();
28InBlock.gif            for (int i = 0; i < CompareCount; i++)
29ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
30InBlock.gif                int o = (int)table1[i];
31ExpandedSubBlockEnd.gif            }

32InBlock.gif            stopWatch1.Stop();
33InBlock.gif            string t2 = stopWatch1.ElapsedTicks.ToString();
34InBlock.gif            Hashtable table2 = new Hashtable();
35InBlock.gif            
36InBlock.gif            for (int i = 0; i < CompareCount; i++)
37ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
38InBlock.gif                ITest test2 = new Test1();
39InBlock.gif                table2.Add(i,test2);
40ExpandedSubBlockEnd.gif            }

41InBlock.gif            
42InBlock.gif            System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
43InBlock.gif            stopWatch2.Start();
44InBlock.gif            for (int i = 0; i < CompareCount; i++)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                ITest o = table2[i] as ITest;
47ExpandedSubBlockEnd.gif            }

48InBlock.gif            stopWatch2.Stop();
49InBlock.gif            string t3 = stopWatch2.ElapsedTicks.ToString();
50InBlock.gif            Console.WriteLine(t1);
51InBlock.gif            Console.WriteLine(t2);
52InBlock.gif            Console.WriteLine(t3);
53InBlock.gif            Console.WriteLine(((double)Convert.ToInt64(t1)/Convert.ToInt64(t2)).ToString());
54InBlock.gif            Console.WriteLine(((double)Convert.ToInt64(t3) / Convert.ToInt64(t2)).ToString());
55InBlock.gif            Console.Read();
56InBlock.gif            
57ExpandedBlockEnd.gif        }


测试结果 

Add

1)  调试(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

227960

1

class

100000

138122

0.6059

Interface

100000

103693

0.4549

 





 
调试(2

数据类型

循环次数

执行时间

执行时间比例

int

100000

282564

1

class

100000

156588

0.5542

Interface

100000

148623

0.5230






2) 

   运行(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

155927

1

class

100000

191537

1.2284

Interface

100000

127647

0.8186

    

 



 
运行(2)   

数据类型

循环次数

执行时间

执行时间比例

int

100000

151806

1

class

100000

222375

1.4649

Interface

100000

256467

1.6894

   





运行
(3)

数据类型

循环次数

执行时间

执行时间比例

int

100000

99465

1

class

100000

235016

2.3628

Interface

100000

201519

2.0260

 

 





从上面几个表可以得出,在向
Hashtable里面添加数据的时候,当value为值类型的时候最快,interface次之,class

 

2. 查询

  

1.       调试(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

52360

1

class

100000

71250

1.3608

Interface

100000

291566

5.5685

 

2.      



调试
(2)

数据类型

循环次数

执行时间

执行时间比例

int

100000

53645

1

class

100000

55679

1.0379

Interface

100000

310780

5.7932

 





  运行
(1)

数据类型

循环次数

执行时间

执行时间比例

int

100000

53013

1

class

100000

55414

1.0453

Interface

100000

282835

5.3352






  运行
(2)

数据类型

循环次数

执行时间

执行时间比例

int

100000

53647

1

class

100000

66768

1.2446

Interface

100000

204599

3.8138






  在查询哈希表的时候,
int最快,class次之,interface比较慢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值