Delphi: fast Pos with 64-bit

前端 未结 2 528
感动是毒
感动是毒 2020-12-15 14:29

Is there any code for a Pos() version that\'s as fast in 64-bit than the current 32-bit?

To my understanding, the 32-bit version in Delphi (tested up to XE5) adopted

2条回答
  •  温柔的废话
    2020-12-15 14:57

    The C runtime function wcsstr as implemented in the system provided msvcrt.dll is better than Delphi RTL Pos for 64 bit code.

    {$APPTYPE CONSOLE}
    
    uses
      SysUtils, Diagnostics;
    
    function wcsstr(const str, strsearch: PWideChar): PWideChar; cdecl; external 'msvcrt.dll';
    
    var
      i, j: Integer;
      Stopwatch: TStopwatch;
      str: string;
      P: PWideChar;
    
    const
      N = 50000000;
    
    begin
      str := 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do '
        + 'eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim '
        + 'ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut '
        + 'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit '
        + 'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur '
        + 'sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt '
        + 'mollit anim id est laborum.';
    
      Stopwatch := TStopwatch.StartNew;
      for i := 1 to N do
      begin
        j := Pos('tempor', str);
        if j=0 then
          Beep;
      end;
      Writeln('Pos: ' + IntToStr(Stopwatch.ElapsedMilliseconds));
    
      Stopwatch := TStopwatch.StartNew;
      for i := 1 to N do
      begin
        P := wcsstr(PChar(str), 'tempor');
        if P=nil then
          Beep;
      end;
      Writeln('wcsstr: ' + IntToStr(Stopwatch.ElapsedMilliseconds));
    
      Readln;
    end.
    

    32 bit release build

    Pos: 1930
    wcsstr: 6951
    

    64 bit release build

    Pos: 18384
    wcsstr: 6701
    

    Interestingly, the 32 bit Pos is clearly very well implemented.

    My system is running Win7 x64 on i5-2300.

提交回复
热议问题