測試Delphi XE4的字串函數與正則表示法速度。

Function time: 46, Count=2023
RegEX time: 117, Count=2023

  

program Project1;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.Classes,
  System.SysUtils,
  RegularExpressionsCore,
  System.StrUtils,
  System.DateUtils;

var
  t0, t1: Double;
  TS: TStringList;
  RegEX: TPerlRegEx;
  I: Integer;
  Count: Cardinal;

begin
  TS := TStringList.Create;
  RegEX := TPerlRegEx.Create;
  Count := 0;

  try
    try
      TS.LoadFromFile('pattern.txt');

      t0 := Now;
      for I := 0 to TS.Count - 1 do
        if AnsiContainsText(TS[I], '.jpg') then
          Inc(Count);
      WriteLn(Format('Function time: %d, Count=%d', [MilliSecondsBetween(t0, Now), Count]));

      Count := 0;

      t0 := Now;
      RegEX.RegEx := '\.(JPG|jpg)';
      for I := 0 to TS.Count - 1 do
      begin
        RegEX.Subject := TS[i];
        if RegEX.Match then
          Inc(Count);
      end;
      WriteLn(Format('RegEX time: %d, Count=%d', [MilliSecondsBetween(t0, Now), Count]));
    except
      on E: Exception do
        WriteLn(E.ClassName, ': ', E.Message);
    end;
  finally
    TS.Free;
    RegEX.Free;
  end;
end.

全站熱搜

bagatelles 發表在 痞客邦 留言(0) 人氣()