zoukankan      html  css  js  c++  java
  • .Net 3.5环境下常用数组性能测试

    这件事情似乎很无聊,但是没人做,我来做下苦力吧。

    以下是一些简单的测试。单位以ms计算。注意里面用到循环的数量有些事1W,有些是100W。

    这些记录可以作为编程过程中的一些参考。

    测试平台:

    奔腾1.6G 双核CPU
    1G内存
    vs2008 调试环境测试。


    一、ArrayList (100W,1W)

                Stopwatch timer = new Stopwatch();
                timer.Start();

                System.Collections.ArrayList al 
    = new System.Collections.ArrayList();

                
    for (int i = 0; i < 1000000; i++)
                {
                    al.Add(i);
                }

                timer.Stop();

                Console.Write(timer.ElapsedMilliseconds.ToString() 
    + "\t");

                Stopwatch timer1 
    = new Stopwatch();
                timer1.Start();

                
    for (int i = 0; i < 10000; i++)
                {
                    al.Contains(i);
                }

                timer1.Stop();

                Console.WriteLine(timer1.ElapsedMilliseconds.ToString());

    98    775
    160    891
    107    773
    193    769


    二、Hashtable (100W,1W)

                Stopwatch timer = new Stopwatch();
                timer.Start();

                System.Collections.Hashtable ht 
    = new System.Collections.Hashtable();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Add(i, i);
                }

                timer.Stop();

                Console.Write(timer.ElapsedMilliseconds.ToString() 
    + "\t");

                Stopwatch timer1 
    = new Stopwatch();
                timer1.Start();

                
    for (int i = 0; i < 10000; i++)
                {
                    ht.ContainsKey(i);
                }

                timer1.Stop();

                Console.WriteLine(timer1.ElapsedMilliseconds.ToString());
            }

    375 0
    673 0
    540 0
    495 0

    把timer1提高到100万(Hashtable (100W,100W))

    389 139
    616 277
    516 140
    610 277

    三、HashSet (100W,100W)

    Stopwatch timer = new Stopwatch();
                timer.Start();

                System.Collections.Generic.HashSet
    <int> ht = new System.Collections.Generic.HashSet<int>();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Add(i);
                }

                timer.Stop();

                Console.Write(timer.ElapsedMilliseconds.ToString() 
    + "\t");

                Stopwatch timer1 
    = new Stopwatch();
                timer1.Start();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Contains(i);
                }

                timer1.Stop();

                Console.WriteLine(timer1.ElapsedMilliseconds.ToString());



    89 32
    79 32
    79 32
    117 31

    四、List (100W,1W)

                Stopwatch timer = new Stopwatch();
                timer.Start();

                System.Collections.Generic.List
    <int> ht = new System.Collections.Generic.List<int>();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Add(i);
                }

                timer.Stop();

                Console.Write(timer.ElapsedMilliseconds.ToString() 
    + "\t");

                Stopwatch timer1 
    = new Stopwatch();
                timer1.Start();

                
    for (int i = 0; i < 10000; i++)
                {
                    ht.Contains(i);
                }

                timer1.Stop();

                Console.WriteLine(timer1.ElapsedMilliseconds.ToString());


    16 379
    19 392
    18 403
    18 392

    把List<int>换成List<object>

    96 945
    157 1033
    106 909
    193 910

    换成string,i.ToString()

    496 1238
    531 1190
    572 1246
    536 1258

    五、Dictionary (100W,100W)

                Stopwatch timer = new Stopwatch();
                timer.Start();

                System.Collections.Generic.Dictionary
    <intint> ht = new System.Collections.Generic.Dictionary<intint>();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Add(i, i);
                }

                timer.Stop();

                Console.Write(timer.ElapsedMilliseconds.ToString() 
    + "\t");

                Stopwatch timer1 
    = new Stopwatch();
                timer1.Start();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.ContainsKey(i);
                }

                timer1.Stop();

                Console.WriteLine(timer1.ElapsedMilliseconds.ToString());

    113 35
    125 34
    124 34
    126 34


    六、Dictionary Linq查询 (100W,100W)

    用个Linq试试
                Stopwatch timer = new Stopwatch();
                timer.Start();

                System.Collections.Generic.Dictionary
    <intint> ht = new System.Collections.Generic.Dictionary<intint>();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Add(i, i);
                }

                timer.Stop();

                Console.Write(timer.ElapsedMilliseconds.ToString() 
    + "\t");

                Stopwatch timer1 
    = new Stopwatch();
                timer1.Start();

                
    for (int i = 0; i < 1000000; i++)
                {
                    ht.Where(c 
    => c.Key == i);
                }

                timer1.Stop();

                Console.WriteLine(timer1.ElapsedMilliseconds.ToString());


    112 177
    107 78
    125 70
    107 82

    结论:
    1、如果是使用缓存的话,那么3.5带来的单泛型集合的HashSet可以替代List了。虽然载入速度慢一点,但是查询速度要比List泛型快很多。要注意到,上述测试List的查询时万级的,而HashSet是百万级

    2、Dictionary泛型可以替换掉Hashtable了,虽然如果在字符或者object类型下可能会没这么明显。但是在数字类型的匹配上,Dictionary比Hashtable大概快了2倍,而这个开销估计是Hashtable的装箱造成的。

    3、Linq还是要慢一些。

    by yurow. http://www.cnblogs.com/birdshover/
  • 相关阅读:
    XAF 有条件的对象访问权限
    XAF 顯示 UnInplace Report(設置自定義條件顯示報表,不是根據選擇ListView記錄條件顯示報表)
    XAF 如何自定义PivotGrid单元格显示文本?
    XAF 如何布局详细视图上的按钮
    XAF How to set size of a popup detail view
    XAF Delta Replication Module for Devexpress eXpressApp Framework
    XAF 帮助文档翻译 EasyTest Basics(基础)
    XAF 用户双击ListView记录时禁止显示DetailView
    XAF How to enable LayoutView mode in the GridControl in List Views
    XAF 如何实现ListView单元格批量更改?
  • 原文地址:https://www.cnblogs.com/birdshover/p/1113949.html
Copyright © 2011-2022 走看看