DataTable和普通类型存储数据,内存和效率的差别
2011-01-14 14:17 爱车龟速兔 阅读(424) 评论(0) 编辑 收藏 举报测试了一下普通的dt和类型的速度差异, 下面是代码:
代码
public class MrInfo
{
public int versionid { get; set; }
public string MCellId { get; set; }
public int NCell_Bcch { get; set; }
public int NCell_Bsic { get; set; }
public double RMS0 { get; set; }
public double RMS1 { get; set; }
public double RMS2 { get; set; }
public double RMS3 { get; set; }
public double RMS4 { get; set; }
public double RMS5 { get; set; }
public double RMS6 { get; set; }
public double RMS7 { get; set; }
public double RMS8 { get; set; }
public double RMS9 { get; set; }
public double Total { get; set; }
public DateTime CollectTime { get; set; }
}
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;
}
}
{
public int versionid { get; set; }
public string MCellId { get; set; }
public int NCell_Bcch { get; set; }
public int NCell_Bsic { get; set; }
public double RMS0 { get; set; }
public double RMS1 { get; set; }
public double RMS2 { get; set; }
public double RMS3 { get; set; }
public double RMS4 { get; set; }
public double RMS5 { get; set; }
public double RMS6 { get; set; }
public double RMS7 { get; set; }
public double RMS8 { get; set; }
public double RMS9 { get; set; }
public double Total { get; set; }
public DateTime CollectTime { get; set; }
}
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 |
|