• How to read very large text files fast


    Question
    Does anyone know the fastest way to read large text files (10Mb) into a string.Readln is just too slow.
     

    Answer 1

    You may try this:

    function R(const FileName: string): string;
    var
      M: TFileStream;
    begin
      M := TFileStream.Create(FileName, fmOpenRead);
      try
        SetLength(Result, M.Size);
        M.Read(Result[1], M.Size);
      finally
        M.Free;
      end;
    end;

    Answer 2

    As an alternative to Christian's suggestion, you can also use a memory-mapped file:

    function MMFileToString(const AFilename: string): string;
    var
      hFile: THandle;
      hFileMap: THandle;
      hiSize: DWORD;
      loSize: DWORD;
      text: string;
      view: pointer;
    begin
      Result := '';
      if AFilename = '' then
        Exit;
      if not FileExists(AFilename) then
        Exit;
      {Open the file}
      hFile := CreateFile(
        PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
      );
      if hFile <> INVALID_HANDLE_VALUE then
      begin
        loSize := GetFileSize(hFile, @hiSize);
        {File was opened successfully, now map it:}
        hFileMap := CreateFileMapping(
          hFile, nil, PAGE_READONLY, hiSize, loSize, 'TextForString'
        );
        if (hFileMap <> 0) then
        begin
          if (GetLastError() = ERROR_ALREADY_EXISTS) then
          begin
            MessageDlg(
              'Mapping already exists - not created.', mtWarning, [mbOk], 0
            );
            CloseHandle(hFileMap)
          end
          else
          begin
            try
              {File mapped successfully, now map a view of the file into the
              address space:}
              view := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
              if (view <> nil) then
              begin {View mapped successfully}
                {Close file handle - as long is view is open it will persist}
                CloseHandle(hFile);
                SetLength(Result, loSize);
                Move(view^, Result[1], loSize);
              end
              else
                MessageDlg(
                  'Unable to map view of file. ' + SysErrorMessage(GetLastError),
                  mtWarning, [mbOk], 0
                );
            finally
              UnmapViewOfFile(view);  {Close view}
              CloseHandle(hFileMap);  {Close mapping}
            end
          end
        end
        else
        begin
          MessageDlg(
            'Unable to create file mapping. ' + SysErrorMessage(GetLastError),
            mtWarning, [mbOk], 0
          );
        end;
      end
      else
      begin
        MessageDlg(
          'Unable to open file. ' + SysErrorMessage(GetLastError),
          mtWarning, [mbOk], 0
        );
      end;
    end;
  • 相关阅读:
    洛谷 P1879 [USACO06NOV]玉米田Corn Fields
    洛谷 P2709 小B的询问
    洛谷 P1972 [SDOI2009]HH的项链
    洛谷 P3648 [APIO2014]序列分割
    洛谷 P2157 [SDOI2009]学校食堂
    洛谷 P1198 [JSOI2008]最大数
    洛谷 P3870 [TJOI2009]开关
    【模板】线段树2
    【模板】线段树1
    git之远程标签下载(远程分支)
  • 原文地址:https://www.cnblogs.com/shangdawei/p/4036500.html
Copyright © 2020-2023  润新知