PAIP.提升性能---LISTBOX加载30万大数据量终结方案
2013-04-06 16:32
197 查看
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++;
}));
}
}
相关文章推荐
- ACCESS大批量数据写入之性能提升方案
- ACCESS大批量数据写入之性能提升方案
- ACCESS大批量数据写入之性能提升方案
- ACCESS大批量数据写入之性能提升方案
- ACCESS大批量数据写入之性能提升方案
- paip.提升性能----SQL优化总结
- Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案
- 缓存Integer等类型的频繁使用的数据和对象,大幅度提升性能
- paip.提升性能----jvm参数调整.txt
- paip.提升性能---mysql 性能 测试以及 参数调整.txt
- 网页速度很慢优化方案:如何提高网页加载速度,提升网站加载速度
- javascript性能提升——动态加载脚本
- 数据加载的性能测试
- OpenJDK源码研究笔记(五)-缓存Integer等类型的频繁使用的数据和对象,大幅度提升性能(一道经典的Java笔试题)
- paip.提升性能--- mysql 建立索引 删除索引 很慢的解决.
- Atitit.h5 web webview性能提升解决方案-----fileStrore缓存离线存储+http方案
- paip.提升性能---.net listbox 使用BeginUpdate与EndUpdate
- 【Android性能优化】Android图片加载方案--Bitmap的内存管理和优化方案
- MySql 大数据处理 性能提升 日志清理
- paip.提升性能---- 网站并发数的总结.txt