How to spead a loop?

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 8
Reputation: marygreen is an unknown quantity at this point 
Solved Threads: 0
marygreen marygreen is offline Offline
Newbie Poster

How to spead a loop?

 
-1
  #1
Sep 17th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 856
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 139
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: How to spead a loop?

 
0
  #2
Sep 17th, 2009
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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 8
Reputation: marygreen is an unknown quantity at this point 
Solved Threads: 0
marygreen marygreen is offline Offline
Newbie Poster

Re: How to spead a loop?

 
0
  #3
Sep 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 452
Reputation: FlamingClaw will become famous soon enough FlamingClaw will become famous soon enough 
Solved Threads: 108
FlamingClaw's Avatar
FlamingClaw FlamingClaw is offline Offline
Posting Pro in Training

Re: How to spead a loop?

 
0
  #4
Sep 19th, 2009
"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:
  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
Be a good part of the community.Don't be ungrateful.
If you ask something on the forum and you got the right answer then mark as solved!
If my opinion helped to you a lot then sometimes give reputation point to me.
I'm just a pascal programmer from Hungary.
Farewell...
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 8
Reputation: marygreen is an unknown quantity at this point 
Solved Threads: 0
marygreen marygreen is offline Offline
Newbie Poster

Re: How to spead a loop?

 
0
  #5
Sep 19th, 2009
FlamingClaw,

wow, that's one clean and elegant piece of code. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 1
Reputation: jng is an unknown quantity at this point 
Solved Threads: 0
jng jng is offline Offline
Newbie Poster

Re: How to spead a loop?

 
0
  #6
Sep 23rd, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC