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

2008-02-22 09:39:30来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

首先定义两个类:
1 public interface ITest
2 {
3 void M();
4 }
5 public class Test1:ITest
6 {
7 public void M()
8 {
9 }
10 }
11 class Test
12 {
13 public Test()
14 {
15 }
16 } 首先,测试设置的速度hashtable.add()
1static void Main(string[] args)
2 {
3 Hashtable table = new Hashtable();
4
5 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
6 stopWatch.Start();
7 for (int i = 0; i < CompareCount; i )
8 {
9 table.Add(i,new Test());
10 }
11 stopWatch.Stop();
12
13 for (int i = 0; i < CompareCount; i )
14 {
15 Test o = table[i] as Test;
16 }
17
18 string t1 = stopWatch.ElapsedTicks.ToString();
19
20 Hashtable table1 = new Hashtable();
21 System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
22 stopWatch1.Start();
23 for (int i = 0; i < CompareCount; i )
24 {
25 table1.Add(i, i);
26 }
27 stopWatch1.Stop();
28
29 for (int i = 0; i < CompareCount; i )
30 {
31 int o = (int)table1[i];
32 }
33
34 string t2 = stopWatch1.ElapsedTicks.ToString();
35 Hashtable table2 = new Hashtable();
36 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
37 stopWatch2.Start();
38 for (int i = 0; i < CompareCount; i )
39 {
40 ITest test2 = new Test1();
41 table2.Add(i,test2);
42 }
43
44 stopWatch2.Stop();
45 for (int i = 0; i < CompareCount; i )
46 {
47 ITest o = table2[i] as ITest;
48 }
49
50 string t3 = stopWatch2.ElapsedTicks.ToString();
51 Console.WriteLine(t1);
52 Console.WriteLine(t2);
53 Console.WriteLine(t3);
54 Console.WriteLine(((double)Convert.ToInt64(t1)/Convert.ToInt64(t2)).ToString());
55 Console.WriteLine(((double)Convert.ToInt64(t3) / Convert.ToInt64(t2)).ToString());
56 Console.Read();
57
58 }测试获取的代码
1static void Main(string[] args)
2 {
3 Hashtable table = new Hashtable();
4
5 for (int i = 0; i < CompareCount; i )
6 {
7 table.Add(i,new Test());
8 }
9
10 System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
11 stopWatch.Start();
12 for (int i = 0; i < CompareCount; i )
13 {
14 Test o = table[i] as Test;
15 }
16 stopWatch.Stop();
17 string t1 = stopWatch.ElapsedTicks.ToString();
18
19 Hashtable table1 = new Hashtable();
20
21 for (int i = 0; i < CompareCount; i )
22 {
23 table1.Add(i, i);
24 }
25
26 System.Diagnostics.Stopwatch stopWatch1 = new System.Diagnostics.Stopwatch();
27 stopWatch1.Start();
28 for (int i = 0; i < CompareCount; i )
29 {
30 int o = (int)table1[i];
31 }
32 stopWatch1.Stop();
33 string t2 = stopWatch1.ElapsedTicks.ToString();
34 Hashtable table2 = new Hashtable();
35
36 for (int i = 0; i < CompareCount; i )
37 {
38 ITest test2 = new Test1();
39 table2.Add(i,test2);
40 }
41
42 System.Diagnostics.Stopwatch stopWatch2 = new System.Diagnostics.Stopwatch();
43 stopWatch2.Start();
44 for (int i = 0; i < CompareCount; i )
45 {
46 ITest o = table2[i] as ITest;
47 }
48 stopWatch2.Stop();
49 string t3 = stopWatch2.ElapsedTicks.ToString();
50 Console.WriteLine(t1);
51 Console.WriteLine(t2);
52 Console.WriteLine(t3);
53 Console.WriteLine(((double)Convert.ToInt64(t1)/Convert.ToInt64(t2)).ToString());
54 Console.WriteLine(((double)Convert.ToInt64(t3) / Convert.ToInt64(t2)).ToString());
55 Console.Read();
56
57 }

测试结果

Add

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何改变asp.net项目名称

下一篇:ASP.NET弹出一个对话框