Test1:将5个字符串放到DataRow里,再把DataRow放入Table中,每次要1321.9008ms
Test2:将5个字符串放到类A中,再将A放到ArrayList中,每次要185.2664ms
Test3:将5个字符串放到类A中,再将A放到DataRow、Table中,每次要521.75024ms
Test4:将1个字符串放到DataRow里,再把DataRow放入Table中,每次要252.36288ms
结论(乱写的):
1.DataTable确实比较慢,有人说是MarshalRefObject的原因,也许吧(可能是7:1的关系)。
2.DataTable的快慢与列的数目有关,列数与快慢成正比(看起来像是正比,Test1=Test4*5)。
3.在DataTable中,把5个字符串分散放到5列 要比 把5个字符串一起放到1列慢
using System;
using System.Diagnostics;
using System.Collections;
using System.Data;
namespace ConsoleApplication9
{
[Serializable]
class A
{
public string m1;
public string m2;
public string m3;
public string m4;
public string m5;
}
class B
{
public static DataTable F()
{
DataTable dt = new DataTable();
dt.Columns.Add("m1",typeof(string));
dt.Columns.Add("m2",typeof(string));
dt.Columns.Add("m3",typeof(string));
dt.Columns.Add("m4",typeof(string));
dt.Columns.Add("m5",typeof(string));
return dt;
}
public static DataTable F1()
{
DataTable dt = new DataTable();
dt.Columns.Add("m1",typeof(string));
return dt;
}
public static DataTable F2()
{
DataTable dt = new DataTable();
dt.Columns.Add("A",typeof(A));
return dt;
}
}
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Test4();
}
private static void Test1()
{
DataTable dt = B.F();
double pj = 0;
for(int j=0; j<10; j++)
{
DateTime time = DateTime.Now;
for(int i=0; i<50000; i++)
{
DataRow dr = dt.NewRow();
dr["m1"] = i.ToString();
dr["m2"] = i.ToString();
dr["m3"] = i.ToString();
dr["m4"] = i.ToString();
dr["m5"] = i.ToString();
dt.Rows.Add(dr);
}
TimeSpan span = DateTime.Now - time;
double tmp = span.TotalMilliseconds;
Debug.WriteLine(tmp);
pj += tmp;
dt.Rows.Clear();
}
Debug.WriteLine("");
Debug.WriteLine(pj/10);
}
private static void Test2()
{
ArrayList ar = new ArrayList();
double pj = 0;
for(int j=0; j<10; j++)
{
DateTime time = DateTime.Now;
for(int i=0; i<50000; i++)
{
A a = new A();
a.m1 = i.ToString();
a.m2 = i.ToString();
a.m3 = i.ToString();
a.m4 = i.ToString();
a.m5 = i.ToString();
ar.Add(a);
}
TimeSpan span = DateTime.Now - time;
double tmp = span.TotalMilliseconds;
Debug.WriteLine(tmp);
pj += tmp;
ar.Clear();
}
Debug.WriteLine("");
Debug.WriteLine(pj/10);
}
private static void Test3()
{
DataTable dt = B.F2();
double pj = 0;
for(int j=0; j<10; j++)
{
DateTime time = DateTime.Now;
for(int i=0; i<50000; i++)
{
A a = new A();
a.m1 = i.ToString();
a.m2 = i.ToString();
a.m3 = i.ToString();
a.m4 = i.ToString();
a.m5 = i.ToString();
DataRow dr = dt.NewRow();
dr["A"] = a;
dt.Rows.Add(dr);
}
TimeSpan span = DateTime.Now - time;
double tmp = span.TotalMilliseconds;
Debug.WriteLine(tmp);
pj += tmp;
dt.Rows.Clear();
}
Debug.WriteLine("");
Debug.WriteLine(pj/10);
}
private static void Test4()
{
DataTable dt = B.F1();
double pj = 0;
for(int j=0; j<10; j++)
{
DateTime time = DateTime.Now;
for(int i=0; i<50000; i++)
{
DataRow dr = dt.NewRow();
dr["m1"] = i.ToString();
dt.Rows.Add(dr);
}
TimeSpan span = DateTime.Now - time;
double tmp = span.TotalMilliseconds;
Debug.WriteLine(tmp);
pj += tmp;
dt.Rows.Clear();
}
Debug.WriteLine("");
Debug.WriteLine(pj/10);
}
}
}
=====================================================
结果:
------------------
Test1():
1462.1024
1221.7568
1592.2896
1131.6272
1482.1312
1341.9296
1271.8288
1241.7856
1151.656
1321.9008
(1)1321.9008
1452.088
1191.7136
1562.2464
1111.5984
1512.1744
1392.0016
1331.9152
1452.088
1101.584
1472.1168
(2)1357.95264
1371.9728
1171.6848
1522.1888
1061.5264
1412.0304
1371.9728
1301.872
1412.0304
1071.5408
1371.9728
(3)1306.8792
Test2():
170.2448
250.36
210.3024
160.2304
170.2448
150.216
200.288
150.216
160.2304
230.3312
(1)185.2664
230.3312
240.3456
220.3168
160.2304
170.2448
160.2304
200.288
150.216
170.2448
230.3312
(2)193.27792
170.2448
240.3456
200.288
160.2304
170.2448
160.2304
200.288
150.216
170.2448
230.3312
(3)185.2664
Test3():
500.72
430.6192
590.8496
510.7344
470.6768
500.72
510.7344
610.8784
500.72
550.792
(1)517.74448
480.6912
430.6192
590.8496
520.7488
480.6912
500.72
510.7344
620.8928
520.7488
560.8064
(2)521.75024
480.6912
430.6192
600.864
520.7488
480.6912
500.72
510.7344
620.8928
520.7488
580.8352
(2)524.75456
Test4():
270.3888
460.6624
230.3312
230.3312
260.3744
240.3456
240.3456
290.4176
240.3456
240.3456
(1)270.3888
270.3888
450.648
220.3168
200.288
240.3456
210.3024
220.3168
280.4032
210.3024
220.3168
(2)252.36288
260.3744
440.6336
200.288
200.288
240.3456
220.3168
210.3024
270.3888
210.3024
220.3168
(3)247.35568