[fpc-pascal] Record helpers on built in types?

Anthony Walter sysrpl at gmail.com
Wed Feb 18 12:29:55 CET 2015


Okay guys, some testing results of my earlier referenced string functions.
I went to this page and copied Moby Dick:

https://www.gutenberg.org/files/2701/2701-h/2701-h.htm

I save that to 'c:\temp\moby-dick.txt' and ran this test (IStopwatch.Time
is seconds elapsed as Double):

procedure TForm1.Button1Click(Sender: TObject);
var
  Stopwatch: IStopwatch;
  B: Boolean;
  S, S1, S2: string;
  T1, T2: Double;
  I: Integer;
begin
  Stopwatch := StopwatchCreate;
  Memo1.Lines.Clear;
  S := FileReadStr('c:\temp\moby-dick.txt');
  Memo1.Lines.AddFormat('Length of moby dick text = {0}', [S.Length]);
  Memo1.Lines.AddLine;
  Stopwatch.Reset;
  S1 := S.Replace('Whale', 'The Big Fish', True);
  Stopwatch.Calculate;
  Stopwatch.Calculate;
  T1 := Stopwatch.Time;
  Memo1.Lines.AddFormat('StringHelper.Replace time = {0}', [T1]);
  Stopwatch.Reset;
  S2 := StringReplace(S, 'Whale', 'The Big Fish', [rfReplaceAll,
rfIgnoreCase]);
  Stopwatch.Calculate;
  T2 := Stopwatch.Time;
  Memo1.Lines.AddFormat('StringReplace time = {0}', [T2]);
  Memo1.Lines.Add('----------------------------');
  Memo1.Lines.AddFormat('Speed increase: {0:2} x faster', [T2 / T1]);
  Memo1.Lines.AddLine;
  Stopwatch.Reset;
  B := S1.Compare(S2) = 0;
  Stopwatch.Calculate;
  T1 := Stopwatch.Time;
  if B then
  Memo1.Lines.AddFormat('StringHelper.Compare time = {0}', [T1]);
  Stopwatch.Reset;
  B := AnsiCompareText(S1, S2) = 0;
  Stopwatch.Calculate;
  T2 := Stopwatch.Time;
  if B then
  Memo1.Lines.AddFormat('AnsiCompareText time = ', [T2]);
  Memo1.Lines.Add('----------------------------');
  Memo1.Lines.AddFormat('Speed increase: {0:2} x faster', [T2 / T1]);
  Memo1.Lines.AddLine;
  Stopwatch.Reset;
  I := S.MatchCount('Whale', True);
  Stopwatch.Calculate;
  Memo1.Lines.AddFormat('StringHelper.MatchCount = {0}', [I]);
  Memo1.Lines.AddFormat('StringHelper.MatchCount time = {0}',
[Stopwatch.Time]);
end;

Here are the results:

Length of moby dick text = 1239706

StringHelper.Replace time = 0.0189264156797435
StringReplace time = 0.810222176252864
----------------------------
Speed increase: 42.80 x faster

StringHelper.Compare time = 0.00125797372311354
AnsiCompareText time = 0.010209045431111
----------------------------
Speed increase: 8.11 x faster

StringHelper.MatchCount = 1705
StringHelper.MatchCount time = 0.00554488154011779
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150218/bc08bddb/attachment.html>


More information about the fpc-pascal mailing list