L
lfq
Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序中使用Tclientdataset作为一个临时内存表存储数据,但需要对其中的数据作不断的更新,结果发现随着更新次数的增加,更新速度越来越慢,非常奇怪,我找不到原因,请诸位看看。<br>下面是我写的一个测试程序及测试结果,该程序在delphi7 和 delphi2006 下表现一致。<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs,DBClient, StdCtrls, DB ;<br><br>type<br> TForm1 = class(TForm)<br> memoryDataSet: TClientDataSet;<br> Memo1: TMemo;<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var i:integer;b:tdatetime;<br>begin<br> with MeMoryDataset do<br> begin<br> //创建表结构<br> with FieldDefs.AddFieldDef do<br> begin<br> DataType:=ftString;<br> Size:=20;<br> Name:='S1';<br> end;<br> with FieldDefs.AddFieldDef do<br> begin<br> DataType:=ftFloat;<br> Name:='f1';<br> end;<br> with FieldDefs.AddFieldDef do<br> begin<br> DataType:=ftinteger;<br> Name:='i1';<br> end;<br> createdataset;<br> //打开表,并增加100个记录。<br> memorydataset.Open;<br> for I := 1 to 100 do<br> memorydataset.InsertRecord(['NAME'+inttostr(i),i*0.1,i]);<br> //重复变更所有记录中某一字段的值,并将完成一次的时间显示在memo中<br> for i:=0 to 100 do<br> begin<br> b:=now;<br> with memorydataset do<br> begin<br> first;<br> while not eof do<br> begin<br> edit;<br> fieldbyname('i1').AsInteger:=i;<br> post;<br> next;<br> end;<br> end;<br> memo1.Lines.add(formatdatetime('hh:nn:SS.zzz',(now-b)));<br> end;<br><br> end;<br>end;<br><br>end.<br><br>运行结果如下:<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.015<br>00:00:00.000<br>00:00:00.000<br>00:00:00.016<br>00:00:00.000<br>00:00:00.000<br>00:00:00.000<br>00:00:00.015<br>00:00:00.016<br>00:00:00.000<br>00:00:00.016<br>00:00:00.015<br>00:00:00.016<br>00:00:00.031<br>00:00:00.016<br>00:00:00.031<br>00:00:00.016<br>00:00:00.031<br>00:00:00.031<br>00:00:00.016<br>00:00:00.032<br>00:00:00.031<br>00:00:00.031<br>00:00:00.031<br>00:00:00.031<br>00:00:00.047<br>00:00:00.047<br>00:00:00.047<br>00:00:00.047<br>00:00:00.062<br>00:00:00.047<br>00:00:00.063<br>00:00:00.062<br>00:00:00.063<br>00:00:00.062<br>00:00:00.063<br>00:00:00.078<br>00:00:00.062<br>00:00:00.078<br>00:00:00.078<br>00:00:00.094<br>00:00:00.094<br>00:00:00.093<br>00:00:00.094<br>00:00:00.094<br>00:00:00.094<br>00:00:00.094<br>00:00:00.109<br>00:00:00.157<br>00:00:00.109<br>00:00:00.172<br>00:00:00.125<br>00:00:00.125<br>00:00:00.140<br>00:00:00.141<br>00:00:00.141<br>00:00:00.156<br>00:00:00.141<br>00:00:00.171<br>00:00:00.172<br>00:00:00.157<br>00:00:00.171<br>00:00:00.188<br>00:00:00.187<br>00:00:00.187<br>00:00:00.187<br>00:00:00.250<br>00:00:00.203<br>00:00:00.204<br>00:00:00.234<br>00:00:00.234<br>00:00:00.235<br>00:00:00.234<br>00:00:00.250<br>00:00:00.250<br>00:00:00.250<br>00:00:00.282<br>00:00:00.296<br>00:00:00.282<br>00:00:00.343<br>00:00:00.313<br>00:00:00.312<br>00:00:00.329<br>00:00:00.312<br>00:00:00.313<br>00:00:00.344<br>00:00:00.375<br>00:00:00.359<br>00:00:00.344<br>00:00:00.328<br>00:00:00.359<br>00:00:00.360<br>00:00:00.406<br>00:00:00.406<br>00:00:00.391<br>可以看出,更新速度稳定变慢,原因何在?