博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to read very large text files fast
阅读量:4634 次
发布时间:2019-06-09

本文共 2426 字,大约阅读时间需要 8 分钟。

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;

转载于:https://www.cnblogs.com/yzryc/p/6382451.html

你可能感兴趣的文章
java2
查看>>
复制图片的一部分
查看>>
调试uIP出现死机问题
查看>>
AttributeError: 'dict' object has no attribute 'status_code'
查看>>
poj2135最小费用最大流经典模板题
查看>>
hdu 4355 Party All the Time (2012 Multi-University Training Contest 6 ) 三分搜索
查看>>
POJ 2528 Mayor's posters(线段树)
查看>>
【转】[退役]纪念我的ACM——headacher@XDU
查看>>
利用STl实现队列
查看>>
android中The connection to adb is down,问题和解决 AndroidEclipseAntXML
查看>>
项目需求分析与建议
查看>>
UVa 10112 - Myacm Triangles
查看>>
给同一个按钮添加单双击事件
查看>>
form
查看>>
powershell输出错误信息到文件
查看>>
VS不显示最近打开的项目
查看>>
wcf客户端捕获异常
查看>>
MyEclipse安装Freemarker插件
查看>>
计算多项式的值
查看>>
DP(动态规划)
查看>>