C++Builder 程序员博客
27 Nov
任务:用Memo显示数据,数据分一笔几个字符的添加,每次添加后将滚动条移动到最下,以显示最新的数据。
用BCB写了个程序实测,发现1000次,每次添加111个字符,需要22秒,太慢。
代码:
stemp="testSelTexttestSelTexttestSelTexttestSelTextasdfhapojdfhopahdfpoahsdpfhapsdfhapdhfpqaweypr9qhweuifbqpiwebcvoiqu";
for(int i=0;i <1000;i++)
{
Memo1->SelStart=Memo1->Lines->Text.Length();
Memo1->SelLength=0;
Memo1->SelText=stemp;
}
请教如何编程来提高速度?
这样循环应该快不到哪去吧
不知道你具体想干什么?
我是用在串口调试软件里的,显示接收的串口数据。
每收到一些数据,就往memo里面加,并移动滚动条,以显示最新接收的数据。
请高手支招。
試驗一下
Memo1->Text = Memo1->Text + stemp;
這樣添加數據速度應該不錯
String stemp = "test";
for(int i = 0; i < 1000; i++)
{
stemp += "\r\ntestSelTexttestSelTexttestSelTexttestSelTextasdfhapojdfhopahdfpoahsdpfhapsdfhapdhfpqaweypr9qhweuifbqpiwebcvoiqu";
}
Memo1->Text = stemp;
這個的速度就相當快了,1秒内
//试试 for(int i=0;i <1000;i++) { Memo1->Lines->Add(stemp); Memo1->SelStart=Memo1->Lines->Text.Length(); }
先把memo裏要顯示的内容拼好 放在一個String型變量裏
然後直接用Memo1->Text = String變量
如果要換行 直接用"\r\n"拼上去
刚测试了下,差不多需要1秒
使用以下方式,共耗时2003毫秒。
AnsiString stemp="testSelTexttestSelTexttestSelTexttestSelTextasdfhapojdfhopahdfpoahsdpfhapsdfhapdhfpqaweypr9qhweuifbqpiwebcvoiqu"; for(int i=0;i <1000;i++) { Memo1->Lines->Add(stemp); }
这种方式需要411毫秒,够快了吧!
AnsiString stemp="testSelTexttestSelTexttestSelTexttestSelTextasdfhapojdfhopahdfpoahsdpfhapsdfhapdhfpqaweypr9qhweuifbqpiwebcvoiqu"; Memo1->Lines->BeginUpdate(); for(int i=0;i <1000;i++) { Memo1->Lines->Add(stemp); } Memo1->Lines->EndUpdate();
11楼,你时间咋计算的?有啥插件?
TDateTime t1 = Now();
AnsiString stemp="testSelTexttestSelTexttestSelTexttestSelTextasdfhapojdfhopahdfpoahsdpfhapsdfhapdhfpqaweypr9qhweuifbqpiwebcvoiqu";
Memo1->Lines->BeginUpdate();
for(int i=0;i <1000;i++)
{
Memo1->Lines->Add(stemp);
}
Memo1->Lines->EndUpdate();
TDateTime t2 = Now();
MilliSecondsBetween(t1,t2);
靠,代码里计时?阴险。以为有什么插件
呵呵,赞个
串口的数据是陆续来的,所以必须一点一点的实时显示在Memo里。6楼的做法不适用。
串口的数据不是一行一行来的,这一次的数据需要接着以前的数据来显示。Add(stemp)的方法一加就是一行,接不上。
主楼的做法是接的上,但是速度太慢。
我是用的GetTickCount,两次相减,就是毫秒
难道要用一行来显示所有的数据?用户怎么看?
另外就是用DrawText来输出数据,因为一行的数据修改了,必然会引起控件内容刷新,为了免除不必要的系统刷新引起的消耗,自己绘制才能保证最快。
学习了
Memo会自动换行的。
看来自己绘制可能是必须的。
ADD应该是最快的,大家可以用线程自己试试