• DataTable和普通类型存储数据,内存和效率的差别


    测试了一下普通的dt和类型的速度差异, 下面是代码:

    代码
         public class MrInfo
        {
            
    public int versionid { getset; }
            
    public string MCellId { getset; }
            
    public int NCell_Bcch { getset; }
            
    public int NCell_Bsic { getset; }
            
    public double RMS0 { getset; }
            
    public double RMS1 { getset; }
            
    public double RMS2 { getset; }
            
    public double RMS3 { getset; }
            
    public double RMS4 { getset; }
            
    public double RMS5 { getset; }
            
    public double RMS6 { getset; }
            
    public double RMS7 { getset; }
            
    public double RMS8 { getset; }
            
    public double RMS9 { getset; }
            
    public double Total { getset; }
            
    public DateTime CollectTime { getset; }
        }

        
    class Program
        {
            
    static void Main(string[] args)
            {
                Console.WriteLine(DateTime.Now);
                
    //TestNullableInt();
                
    //List<MrInfo> lstMr = TastClass();
                
    //DataTable dt = TestDataTable();
                Console.WriteLine();
                Console.WriteLine(DateTime.Now);
                Console.WriteLine(
    "==OVER==");
                Console.ReadLine();
            }

            
    private static void TestNullableInt()
            {
                
    int i = 100;
                
    int j = 50;
                Console.WriteLine(
    "i:{0},j:{1}", i, j);
                TestInt(i, 
    ref j);
                Console.WriteLine(
    "TestInt(int i, ref int j) i:{0},j:{1}", i, j);
                
    int? ni = 100;
                
    int? nj = 50;
                Console.WriteLine(
    "ni:{0},nj:{1}", ni.Value, nj.Value);
                TestNullInt(ni, 
    ref nj);
                Console.WriteLine(
    "TestNullInt(int? i, ref int? j) i:{0},j:{1}", ni.Value, nj.Value);
            }

            
    static void TestInt(int i, ref int j)
            {
                i
    ++;
                j
    ++;
            }

            
    static void TestNullInt(int? i, ref int? j)
            {
                i 
    = i.Value + 1;
                j 
    = j.Value + 1;
            }

            
    static List<MrInfo> TastClass()
            {
                List
    <MrInfo> lstInfo = new List<MrInfo>();
                
    for (int i = 0; i <= 4000000; i++)
                {
                    MrInfo mr 
    = new MrInfo()
                    {
                        CollectTime 
    = DateTime.Now,
                        MCellId 
    = "460-00-11111-" + i.ToString("00000"),
                        NCell_Bcch 
    = 6,
                        NCell_Bsic 
    = 7,
                        RMS0 
    = 0,
                        RMS1 
    = 1,
                        RMS2 
    = 2,
                        RMS3 
    = 3,
                        RMS4 
    = 4,
                        RMS5 
    = 5,
                        RMS6 
    = 6,
                        RMS7 
    = 7,
                        RMS8 
    = 8,
                        RMS9 
    = 9,
                        Total 
    = 10,
                        versionid 
    = 1
                    };
                    lstInfo.Add(mr);
                    
    if (i % 100000 == 0)
                        Console.Write(
    "^");
                }
                
    return lstInfo;
            }

            
    static DataTable TestDataTable()
            {
                DataTable dt 
    = GetMRTableSchema();
                
    for (int i = 0; i <= 3000000; i++)
                {
                    DataRow dr 
    = dt.NewRow();
                    dr[
    0= 1;
                    dr[
    1= "460-00-11111-" + i.ToString("00000");
                    dr[
    2= 6;
                    dr[
    3= 7;
                    
    for (int j = 0; j < 11; j++)
                    {
                        dr[
    4 + j] = j;
                    }
                    dr[
    "CollectTime"= DateTime.Now;
                    dt.Rows.Add(dr);
                    
    if (i % 100000 == 0)
                        Console.Write(
    "^");
                }
                
    return dt;
            }

            
    static DataTable GetMRTableSchema()
            {
                DataTable mrDataTable 
    = new DataTable();
                mrDataTable.Columns.Add(
    "versionid"typeof(Int32));
                mrDataTable.Columns.Add(
    "MCellId"typeof(String));
                mrDataTable.Columns.Add(
    "NCell_Bcch"typeof(Int32));
                mrDataTable.Columns.Add(
    "NCell_Bsic"typeof(Int32));
                mrDataTable.Columns.Add(
    "RMS0"typeof(double));
                mrDataTable.Columns.Add(
    "RMS1"typeof(double));
                mrDataTable.Columns.Add(
    "RMS2"typeof(double));
                mrDataTable.Columns.Add(
    "RMS3"typeof(double));
                mrDataTable.Columns.Add(
    "RMS4"typeof(double));
                mrDataTable.Columns.Add(
    "RMS5"typeof(double));
                mrDataTable.Columns.Add(
    "RMS6"typeof(double));
                mrDataTable.Columns.Add(
    "RMS7"typeof(double));
                mrDataTable.Columns.Add(
    "RMS8"typeof(double));
                mrDataTable.Columns.Add(
    "RMS9"typeof(double));
                mrDataTable.Columns.Add(
    "Total"typeof(double));
                mrDataTable.Columns.Add(
    "CollectTime", Type.GetType("System.DateTime"));
                
    return mrDataTable;
            }
        }
     
    及时结果差异巨大:
    第一列是内存(M)第二列是执行时间(S)
     

    DT

     

    1百万

    330

    13

     

    333

    12

     

    366

    12

     

    366

    11

     

    368

    12

     

     

    2百万

    604

    26

     

    600

    25

     

    670

    26

     

    688

    26

     

     

     

     

     

    3百万

    1200

    37

     

    1200

    37

     

    1200

    37

     

     

     

     

     

     

     

     

    4百万

    1200

    41

     

    1200

    50

     

     

     

     

     

     

    Class

     

    1百万

    205

    3

     

    206

    3

     

    205

    3

     

    205

    3

     

    205

    3

     

     

    2百万

    387

    6

     

    388

    6

     

    388

    7

     

    389

    6

     

    389

    6

     

     

    3百万

    568

    10

     

    570

    9

     

    570

    9

     

    570

    9

     

    571

    10

     

     

    4百万

    742

    12

     

    744

    11

     

    743

    12

     

  • 相关阅读:
    JS中document对象和window对象有什么区别
    jQuery parent.append和$after的区别
    使用CFURLCreateStringByAddingPercentEscapes进行URL编码
    JQuery中==与===、$("#")与$("")的区别
    理解JavaScript中的arguments,callee,caller,apply
    使用自己的ClassLoader实现热替换
    TextBox 英文文档
    easyui的textbox和validatebox的 赋值区别
    jquery的$.extend、$.fn.extend、 jQuery.extend( target, object1, [objectN])作用及区别
    jQuery 遍历
  • 原文地址:https://www.cnblogs.com/dunnice/p/1935553.html
Copyright © 2020-2023  润新知