PAIP.提升性能---LISTBOX加载30万大数据量终结方案
PAIP.提升性能---LISTBOX加载30万大数据量终结方案
作者Attilax , EMAIL:1466519819@qq.com
经过一番奋斗,终于LISTBOX加载30万大数据量有了好的性能方案……
结果如下:
1K,0.6S
1W,5S
10W,48S
这样推算30万数据应该在150秒左右加载完毕,以后台线程的方式加载,前台界面UI不卡…………达到了很高的效率了,
提升性能最关键的地方在于 Thread.Sleep(10);
-------------------------
while (line != null)
{
n++;
if (n % 1000 == 0)
{
Console.WriteLine("--" + n.ToString() + ":" + line);
Thread.Sleep(10);
}
Thread.Sleep得太频繁,则界面UI更流畅,但是后台线程效率就不高了,反之则反之……
于是采用n % 1000 ,让后台线程加载一千数据后切换到UI线程。这样就数据线程加载效率与UI线程流畅度兼顾……达到了很好的效果……
如果全部加载完10W数据再SLEEP,则10W,40S,但是界面UI就卡住了……
如果每加载一条 数据就SLEEP(1),效率就低了5K,10…………
------------源码如下:----------
int loadRecNum = 0;
long curtime = 0;
// long sleepSpan
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)
{
lastUpdateSec = 0;
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;
Form1 fm = (Form1)obj;
while (line != null)
{
// Thread.Sleep(1);
// Console.WriteLine("--" + n.ToString() + ":" + line);
n++;
//write the lie to console window
if (n % 1000 == 0)
{
Console.WriteLine("--" + n.ToString() + ":" + line);
Thread.Sleep(10);
}
add2List(line);
updateConter4listbox();
int readNum = this.settingForm.getReadNum();
if (n >= readNum)
break;
//Read the next line
line = sr.ReadLine();
}
updateConter4listboxLimidtl();
//close the file
sr.Close();
// ListboxEndUpdate();
c45
}
));
t.Name = " --start txt2list thread";
t.IsBackground = true;
t.Start(this);
}
}
int lastUpdateSec = 0;
/// <summary>
/// update per secs
/// </summary>
private void updateConter4listbox()
{
// loadRecNum++;
long nowtime = DateTime.Now.Ticks;
float span = (float)(((float)(nowtime - curtime)) / ((float)10000000));
int spanint = (int)span;
if (spanint <= lastUpdateSec)
return;
else
lastUpdateSec = spanint;
label9.Invoke(new EventHandler(delegate
{
label9.Text = "用 时:" + span.ToString() + "秒";
}));
label8.Invoke(new EventHandler(delegate
{
label8.Text = "已经加载:" + loadRecNum.ToString();
}));
}
private void updateConter4listboxLimidtl()
{
// loadRecNum++;
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() + "秒";
}));
label8.Invoke(new EventHandler(delegate
{
label8.Text = "已经加载:" + loadRecNum.ToString();
}));
}
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++;
}));
}
}
作者Attilax , EMAIL:1466519819@qq.com
经过一番奋斗,终于LISTBOX加载30万大数据量有了好的性能方案……
结果如下:
1K,0.6S
1W,5S
10W,48S
这样推算30万数据应该在150秒左右加载完毕,以后台线程的方式加载,前台界面UI不卡…………达到了很高的效率了,
提升性能最关键的地方在于 Thread.Sleep(10);
-------------------------
while (line != null)
{
n++;
if (n % 1000 == 0)
{
Console.WriteLine("--" + n.ToString() + ":" + line);
Thread.Sleep(10);
}
Thread.Sleep得太频繁,则界面UI更流畅,但是后台线程效率就不高了,反之则反之……
于是采用n % 1000 ,让后台线程加载一千数据后切换到UI线程。这样就数据线程加载效率与UI线程流畅度兼顾……达到了很好的效果……
如果全部加载完10W数据再SLEEP,则10W,40S,但是界面UI就卡住了……
如果每加载一条 数据就SLEEP(1),效率就低了5K,10…………
------------源码如下:----------
int loadRecNum = 0;
long curtime = 0;
// long sleepSpan
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)
{
lastUpdateSec = 0;
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;
Form1 fm = (Form1)obj;
while (line != null)
{
// Thread.Sleep(1);
// Console.WriteLine("--" + n.ToString() + ":" + line);
n++;
//write the lie to console window
if (n % 1000 == 0)
{
Console.WriteLine("--" + n.ToString() + ":" + line);
Thread.Sleep(10);
}
add2List(line);
updateConter4listbox();
int readNum = this.settingForm.getReadNum();
if (n >= readNum)
break;
//Read the next line
line = sr.ReadLine();
}
updateConter4listboxLimidtl();
//close the file
sr.Close();
// ListboxEndUpdate();
c45
}
));
t.Name = " --start txt2list thread";
t.IsBackground = true;
t.Start(this);
}
}
int lastUpdateSec = 0;
/// <summary>
/// update per secs
/// </summary>
private void updateConter4listbox()
{
// loadRecNum++;
long nowtime = DateTime.Now.Ticks;
float span = (float)(((float)(nowtime - curtime)) / ((float)10000000));
int spanint = (int)span;
if (spanint <= lastUpdateSec)
return;
else
lastUpdateSec = spanint;
label9.Invoke(new EventHandler(delegate
{
label9.Text = "用 时:" + span.ToString() + "秒";
}));
label8.Invoke(new EventHandler(delegate
{
label8.Text = "已经加载:" + loadRecNum.ToString();
}));
}
private void updateConter4listboxLimidtl()
{
// loadRecNum++;
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() + "秒";
}));
label8.Invoke(new EventHandler(delegate
{
label8.Text = "已经加载:" + loadRecNum.ToString();
}));
}
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++;
}));
}
}