I'm trying to search for sub-string in large text file.
I found David Heffernan buffered disk access unit.
So i use it like this :
function _FindStrInFile(const AFileName, SubStr: string): string;
var
Stream : TReadOnlyCachedFileStream;
SL : TStringList;
I : Integer;
begin
Result := '';
Stream := TReadOnlyCachedFileStream.Create(AFileName);
try
Stream.Position := 0;
SL := TStringList.Create;
try
SL.LoadFromStream(AFileName);
for I := 0 to SL.Count-1 do begin
if Pos(SubStr, SL[I]) > 0 then begin
Result := SL[I];
Break;
end;
end;
finally
SL.Free;
end;
finally
Stream.Free;
end;
end;
i'm not sure if i use it correctly (buffered disk access) or i make it useless when i load the stream into TStringList
and loop thru it.
because i calculate the time consumed of the above method and the below: and i found the below is faster in milliseconds for the tested file.
function _FindStrInFile(const AFileName, SubStr: string): string;
var
SL : TStringList;
I : Integer;
begin
Result := '';
SL := TStringList.Create;
try
SL.LoadFromFile(AFileName);
for I := 0 to SL.Count-1 do begin
if Pos(SubStr, SL[I]) > 0 then begin
Result := SL[I];
Break;
end;
end;
finally
SL.Free;
end;
end;
Any suggestions/guidance to improve the first function ?