943,975 Members | Top Members by Rank

Ad:
Sep 17th, 2009
-1

How to spead a loop?

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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. begin
  2. var
  3. i:integer;
  4. begin
  5. for i:=1 to 9999999 do
  6. begin
  7. //do something
  8. Memo1.Lines.Add ((floattostr(a1.value))+' + '+(floattostr(a2.value))+' + '+(floattostr(a3.value));
  9. //do something
  10. Memo1.Lines.Add ((floattostr(a1.value))+' + '+(floattostr(a2.value))+' + '+(floattostr(a3.value));
  11. //do something
  12. Memo1.Lines.Add ((floattostr(a1.value))+' + '+(floattostr(a2.value))+' + '+(floattostr(a3.value));
  13. Application.ProcessMessages;
  14. if GetKeyState(VK_Escape) AND 128 = 128 then break;
  15. end;
  16. 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marygreen is offline Offline
8 posts
since Sep 2009
Sep 17th, 2009
0

Re: How to spead a loop?

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.
Sponsor
Featured Poster
Reputation Points: 550
Solved Threads: 731
Bite my shiny metal ass!
pritaeas is offline Offline
4,179 posts
since Jul 2006
Sep 17th, 2009
0

Re: How to spead a loop?

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

Pascal and Delphi Syntax (Toggle Plain Text)
  1. var
  2. keep : textfile;
  3. text : string;
  4. begin
  5. AssignFile(keep, 'c:\lines.txt');
  6. ReWrite(keep);
  7. WriteLn(keep,(floattostr(avf1.value)),' + ',(floattostr(avf2.value), '+', (floattostr(avf2.value) ));
  8. CloseFile(keep);
  9. end;

But the loop is even slower than with memo option.

Format function is new for me, I’ll look into it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marygreen is offline Offline
8 posts
since Sep 2009
Sep 19th, 2009
0

Re: How to spead a loop?

"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:
delphi Syntax (Toggle Plain Text)
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  
  3. var i:integer;
  4. s:string;
  5.  
  6. begin
  7. s:=floattostr(3.14);
  8. for i:=1 to 2000000 do
  9. begin
  10. memo1.Lines.add(s);
  11. end;
  12. end;
it takes 8 minute to me to accomplish...my processor is clear pentium 3000 mhz
Reputation Points: 132
Solved Threads: 138
Posting Pro
FlamingClaw is offline Offline
559 posts
since Feb 2009
Sep 19th, 2009
0

Re: How to spead a loop?

FlamingClaw,

wow, that's one clean and elegant piece of code. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
marygreen is offline Offline
8 posts
since Sep 2009
Sep 23rd, 2009
0

Re: How to spead a loop?

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
jng
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jng is offline Offline
1 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: How can I write a program that writes to a text file?
Next Thread in Pascal and Delphi Forum Timeline: Help with a bunch of TImages





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC