using
System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace dtTest
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("a", typeof(int));
dt.Columns.Add("b", typeof(string));
dt.Columns.Add("c", typeof(string));
for (int i = 0; i < 1000000; i++)//1 million
{
DataRow row = dt.NewRow();
row["a"] = 0;
row["b"] = "2";
row["c"] = "3";
dt.Rows.Add(row);
}
//begin test
//test1
long tick = System.DateTime.Now.Ticks;
DataRow[] rows = dt.Select("a=0");
System.Console.WriteLine((System.DateTime.Now.Ticks - tick));
//test2
tick = System.DateTime.Now.Ticks;
StringBuilder sb = new StringBuilder();
//dt2 = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString() == "0")
sb.Append(dt.Rows[i][0].ToString());
}
System.Console.WriteLine((System.DateTime.Now.Ticks - tick));
}
}
}
behindme(43161308) 14:03:42
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace dtTest
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("a", typeof(int));
dt.Columns.Add("b", typeof(string));
dt.Columns.Add("c", typeof(string));
for (int i = 0; i < 1000000; i++)//1 million
{
DataRow row = dt.NewRow();
row["a"] = 0;
row["b"] = "2";
row["c"] = "3";
dt.Rows.Add(row);
}
//begin test
//test1
long tick = System.DateTime.Now.Ticks;
DataRow[] rows = dt.Select("a=0");
System.Console.WriteLine((System.DateTime.Now.Ticks - tick));
//test2
tick = System.DateTime.Now.Ticks;
StringBuilder sb = new StringBuilder();
//dt2 = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][0].ToString() == "0")
sb.Append(dt.Rows[i][0].ToString());
}
System.Console.WriteLine((System.DateTime.Now.Ticks - tick));
}
}
}
2比1快一倍。
behindme(43161308) 14:04:01
返回的东西不一样,可能觉得没可比性
behindme(43161308) 14:04:52
但如果把test2改成用新一DataTable加入,就在复制到另一个DataTable中,这样1比2快10倍
behindme(43161308) 14:06:01
另外不小心说明一个问题,StringBuiler比string在处理字符串的优势,用string,机子差点跑死掉