demoshop

NEWS

demo, trying to be the best.

站內搜尋載入中...

ArrayList和list<>的速度比較

  • 8165
  • 1

demo之前寫陣列的時候都喜歡用ArrayList,因為它實在是太簡單了,但是最近考量到效能問題,所以對於ArrayList和list<>的速度比較做了個比較。

    直接就來吧以下是測試的原始碼(指示測試code寫的好壞就別在意啦)

    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
    double total = 0;
    double total2 = 0;
    for (int x = 0; x < 10; x++)
    {
        sw.Reset();
     
        sw.Start();             
        
        List<int> al = new List<int>();
        int j = 0;
        for (j = 0; j < 1000000; j++)
            al.Add(j);
        sw.Stop();
        Response.Write(sw.Elapsed.TotalSeconds.ToString() + "<p>");
        total += sw.Elapsed.TotalSeconds;
    }
     
    Response.Write("平均"+total/10 + "<p>");
    for (int x = 0; x < 10; x++)
    {
        sw.Reset();
        sw.Start();
        ArrayList al = new ArrayList();
        int j = 0;
        for (j = 0; j < 1000000; j++)
            al.Add(j);
        sw.Stop();
        Response.Write(sw.Elapsed.TotalSeconds.ToString() + "<p>");
        total += sw.Elapsed.TotalSeconds;
    }
    Response.Write("平均" + total/10 + "<p>");

     

    list<> ArrayList
    0.016195

    0.0272568

    0.0445893

    0.0410549

    0.0430464

    0.0399695

    0.0397436

    0.0391034

    0.0401957

    0.041188

    平均0.03723426

    0.2080258

    0.1832909

    0.1314964

    0.130046

    0.1111647

    0.1076231

    0.1131906

    0.1268922

    0.0982692

    0.1674709

    平均0.17498124

     

    看出差別了吧,知道以後該用什麼了吧