Hello to guys/girls on the forum. Question I have is following. How to speed this loop?

begin
var
i:integer;
begin
for i:=1 to 9999999 do
begin
//do something
Memo1.Lines.Add ((floattostr(a1.value))+' + '+(floattostr(a2.value))+' + '+(floattostr(a3.value));
//do something
Memo1.Lines.Add ((floattostr(a1.value))+' + '+(floattostr(a2.value))+' + '+(floattostr(a3.value));
//do something
Memo1.Lines.Add ((floattostr(a1.value))+' + '+(floattostr(a2.value))+' + '+(floattostr(a3.value));
Application.ProcessMessages;
if GetKeyState(VK_Escape) AND 128 = 128 then break;
end;
end;

The loop is very slow. To generate 2,000,000 lines it needs 2:45 hours. As it continues, it gets slower and slower still.

Any help to this problem of mine is greatly appreciated.

Regards,
marygreen

Recommended Answers

All 5 Replies

Is it required to see those lines as they are generated. If you could send them to a file, you can speed it up. The memo is really awful for this amount of data.

Also, the processmessages slows things down. You could call it every 100 loops instead for example.

In the Lines.Add you use floattostr three times. Have a look at the Format() function.

Pitaeas,
Thank you for answering. Indeed, I tried to send lines to a file with this:

var
keep : textfile;
text   : string;
begin
AssignFile(keep, 'c:\lines.txt');
ReWrite(keep);
WriteLn(keep,(floattostr(avf1.value)),' + ',(floattostr(avf2.value), '+', (floattostr(avf2.value) ));
CloseFile(keep);
end;

But the loop is even slower than with memo option.

Format function is new for me, I’ll look into it.

"The loop is very slow. To generate 2,000,000 lines it needs 2:45 hours. As it continues, it gets slower and slower still."

I wrote a same program like yours.....
what if you leave out floattostr function from the loop?
button1 coded:

procedure TForm1.Button1Click(Sender: TObject);

var i:integer;
    s:string;

begin
  s:=floattostr(3.14);
  for i:=1 to 2000000 do
  begin
    memo1.Lines.add(s);
  end;
end;

it takes 8 minute to me to accomplish...my processor is clear pentium 3000 mhz

FlamingClaw,

wow, that's one clean and elegant piece of code. Thanks.

Most likely the delays deal with the memo box. Each time you add lines the list gets longer. Check the limits in you memo component and perhaps overwrite the handling of the memo field to change the update/display process.

Also, if the float conversions can be done beforehand (in the "do something else") you might increase the speed.

JNG

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.