DataTable和普通类型存储数据,内存和效率的差别
2011-01-14 14:17
260 查看
测试了一下普通的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;
}
}
及时结果差异巨大:
第一列是内存(M)第二列是执行时间(S)
DT
1百万
2百万
3百万
4百万
Class
1百万
2百万
3百万
4百万
代码
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;
}
}
及时结果差异巨大:
第一列是内存(M)第二列是执行时间(S)
DT
1百万
330 | 13 | |
333 | 12 | |
366 | 12 | |
366 | 11 | |
368 | 12 |
604 | 26 | |
600 | 25 | |
670 | 26 | |
688 | 26 | |
1200 | 37 | |
1200 | 37 | |
1200 | 37 | |
1200 | 41 | |
1200 | 50 | |
1百万
205 | 3 | |
206 | 3 | |
205 | 3 | |
205 | 3 | |
205 | 3 |
387 | 6 | |
388 | 6 | |
388 | 7 | |
389 | 6 | |
389 | 6 |
568 | 10 | |
570 | 9 | |
570 | 9 | |
570 | 9 | |
571 | 10 |
742 | 12 | |
744 | 11 | |
743 | 12 |
相关文章推荐
- java中的各种数据类型在内存中存储的方式
- byte类型数据的取值范围及溢出后的强制转换及在内存中的存储结果
- java中的各种数据类型在内存中存储的方式 二
- 关于int类型数据在内存中的高低位存储问题
- c/c++中各数据类型在内存中的存储方式
- Java中基本数据类型的存储方式和相关内存的处理方式
- java中的各种数据类型在内存中存储的方式
- Java中的各种数据类型在内存中存储的方式
- java中的各种数据类型在内存中存储的方式
- js数据类型,及内存存储学习笔记
- java中的基本数据类型和引用数据类型以及它们的存储方式堆内存和栈内存
- Java中基本数据类型的存储方式和相关内存的处理方式(java程序员必读经典)
- java中的各种数据类型在内存中存储的方式
- java 中基本数据类型在内存中占据的存储大小
- 数据在内存中的存储之字符类型
- 初探c语言中各数据类型在内存中的存储方式
- java中的各种数据类型在内存中存储的方式
- java中的各种数据类型在内存中存储的方式
- Java中基本数据类型的存储方式和相关内存的处理方式
- java中的各种数据类型在内存中存储的方式