[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 922
  • Last Modified:

Delphi Loop with IDHTTP

I load a List of website in Memo1 and use this code to find all webpage who in the source as astrology or ASTROLOGY i use this and it very slow someone have a better idea to make it faster ?

procedure TForm1.Button7Click(Sender: TObject);
 var
  S1: TStringList;
i: Integer;
 position : Integer;
  begin
   S1 := TStringList.Create;
    try
     for i:=0 to Memo1.Lines.Count  do
       begin
         try
          S1.Clear;
            label4.Caption:=(IntToStr(i));
               memo1.lines[i] := StringReplace( memo1.lines[i],'http://','',[rfReplaceAll]);
                 S1.Add(IdHttp1.Get('http://'+memo1.Lines[i]));
                     except on e:exception do
S1.Clear;
 end;
  position := AnsiPos('astrology', S1.text);
   if position = 0 then
    else
     memo2.Lines.Add(memo1.Lines[i]);
       label1.Caption:=(IntToStr(memo2.Lines.Count -0));

  position := AnsiPos('ASTROLOGY', S1.text);
   if position = 0 then
    else
     memo2.Lines.Add(memo1.Lines[i]);
      label1.Caption:=(IntToStr(memo2.Lines.Count -0));
        end;

finally
 AssignFile(myFile, 'URL.dat');
  ReWrite(myFile);
   WriteLn(myFile, memo2.text);
    CloseFile(myFile);
      end;
       end;

Thank You
0
Clubreseau
Asked:
Clubreseau
  • 3
1 Solution
 
DropZoneCommented:
Can you give an explanation of what it should do?  Perhaps there's a better approach.

      -dZ.
0
 
DropZoneCommented:
The attached code does only this:
1. Iterates through all the lines in Memo1.
2. Checks if any of them contains the term 'astrology' (case-insensitive).
3. If found, stores the string in the TStringList.
4. When it finishes, saves the list of matches to a file called 'URL.dat'.

If this is all you need, then check if it works as fast as you need it.  If it is still too slow, we can work on some other low level optimisations.

One more thing, are you going to be working with multi-byte strings? I ask because you were using AnsiPos() instead of Pos().  Because of this, my code includes only Ansi String calls.  This may be slower than the standard ASCII calls.

       -dZ.
procedure TForm1.Button7Click(Sender: TObject);
Const
  match_string = 'astrology';
  proto_prefix = 'http://';
  output_file  = 'URL.dat';
var
  S1: TStringList;
  i: Integer;
  str: String;
begin
  S1 := TStringList.Create;
  for i:= 0 to (Memo1.Lines.Count - 1)  do begin
    str := memo1.lines[i]; // Get the next string;
 
    // Check for match
    if ( AnsiContainsText(str, match_string) ) then begin
      // If we found a match, save it.
      S1.Add(AnsiReplaceStr(str, proto_prefix, ''));
    end;
  end;
 
  try
    // Save all matches to a file.
    S1.SaveToFile(output_file);
  finally
    S1.Free();
  end;
end;

Open in new window

0
 
DropZoneCommented:
P.S. You do not need to check for exceptions if you clean up your code to not perform errors.  For instance, your loop starts from 0 to Memo1.Lines.Count, which is one more than the number of items.  Removing unecessary Try/Except makes code faster.

     -dZ.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now