paip.提升性能---.net listbox 使用BeginUpdate与EndUpdate
作者Attilax , EMAIL:1466519819@qq.com
使用默认的 this.listBox1.Items.Add(line);这种方式
结果如下:
1千行数据:1秒
1万行: 3.5秒
10万行: 40秒
查找资料说,使用使用BeginUpdate与EndUpdate 可提高性能。
于是使用了BeginUpdate与EndUpdate
结果如下:
结果如下:
1千行数据:0.5秒
1万行: 4秒
10万行: 44秒
结论:
--------
使用BeginUpdate与EndUpdate 在几千行数量级时,确实可提高性能无一倍……
但在一万级以及十万级数据时,甚至比默认方式更慢个10%……
---------------主要源码如下:-----------
int loadRecNum = 0;
long curtime = 0;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Title = "请选择:";
openFileDlg.Filter = "*.*|*.*";
openFileDlg.ShowDialog();
if (openFileDlg.CheckFileExists)
{
if (openFileDlg.FileName.Equals(""))
return;
String pathYZM = openFileDlg.FileName;
//c452308 add txt2list
Thread t = new Thread(new ParameterizedThreadStart(
delegate(object obj)
{
loadRecNum = 0;
curtime = DateTime.Now.Ticks;
//c45 big txt
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader(pathYZM);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
int n = 0;
// listBox1.Visible = false;
listBox1.Invoke(new EventHandler(delegate
{
this.listBox1.BeginUpdate();
}));
Form1 fm = (Form1)obj;
while (line != null)
{
// Thread.Sleep(5);
// Console.WriteLine("--" + n.ToString() + ":" + line);
n++;
//write the lie to console window
if (n % 1000 == 0)
{
Console.WriteLine("--" + n.ToString() + ":" + line);
//listBox1.Invoke(new EventHandler(delegate
//{
// listBox1.EndUpdate();
// // this.listBox1.BeginUpdate();
//}));
}
add2List(line);
int readNum = this.settingForm.getReadNum();
if (n >= readNum)
break;
//Read the next line
line = sr.ReadLine();
//if(line==null)
// listBox1.Invoke(new EventHandler(delegate
// {
// listBox1.EndUpdate();
// }));
}
// listBox1.EndUpdate();
// listBox1.Visible = true;
//close the file
sr.Close();
ListboxEndUpdate();
////c45
loadRecNum++;
label8.Invoke(new EventHandler(delegate
{
label8.Text = "已经加载:" + loadRecNum.ToString();
}));
long nowtime = DateTime.Now.Ticks;
float span = (float)(((float)(nowtime - curtime)) / ((float)10000000));
int spanint = (int)span;
label9.Invoke(new EventHandler(delegate
{
label9.Text = "用 时:" + span.ToString() + "秒";
}));
}
));
t.Name = " --start txt2list thread";
t.IsBackground = true;
t.Start(this);
}
}
private void ListboxEndUpdate()
{
listBox1.Invoke(new EventHandler(delegate
{
listBox1.EndUpdate();
}));
}
int nc45 = 0;
public void add2List(string line)
{
accFilter4ihush af = new accFilter4ihush();
if (af.filterOK(line))
{
loadRecNum++;
//BeginInvoke
listBox1.Invoke(new EventHandler(delegate
{
// Thread.Sleep(5000);
this.listBox1.Items.Add(line);
// Console.WriteLine("--add2List:" + nc45.ToString() + ":" + line);
nc45++;
}));
// label8.Invoke(new EventHandler(delegate
// {
// label8.Text = "已经加载:" + loadRecNum.ToString();
// }));
//long nowtime= DateTime.Now.Ticks;
//float span = (float)( ((float)( nowtime - curtime))/((float)10000000) );
//int spanint =(int) span;
//label9.Invoke(new EventHandler(delegate
//{
// label9.Text = "用 时:" + spanint.ToString() + "秒";
//}));
}
//this.listView1.Items.Add(item);
}