设为首页 加入收藏 网站搜索 繁體中文 中国建站网 — 站长资源分享平台

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

来源:网络收集 作者:佚名 时间:2007-04-25 23:27:12

首先定义两个类:
 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++)

[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]  下一页

Tags:

  • 好的评价 如果您觉得好,就请您
      0%(0)
  • 差的评价 如果您觉得差,就请您
      0%(0)
  • 相关文章
    广告赞助

    文章随便看看 设计素材 建站学院 网页模板 视频教程

    网友评论

    共有 0 位网友发表了评论,得分 0 分,平均 0 分    查看完整评论

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 1分

    内 容:

             通知管理员 验证码: