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:

  1. function R(const FileName: string): string;
  2. var
  3. M: TFileStream;
  4. begin
  5. M := TFileStream.Create(FileName, fmOpenRead);
  6. try
  7. SetLength(Result, M.Size);
  8. M.Read(Result[1], M.Size);
  9. finally
  10. M.Free;
  11. end;
  12. end;

Answer 2

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

  1. function MMFileToString(const AFilename: string): string;
  2. var
  3. hFile: THandle;
  4. hFileMap: THandle;
  5. hiSize: DWORD;
  6. loSize: DWORD;
  7. text: string;
  8. view: pointer;
  9. begin
  10. Result := '';
  11. if AFilename = '' then
  12. Exit;
  13. if not FileExists(AFilename) then
  14. Exit;
  15. {Open the file}
  16. hFile := CreateFile(
  17. PChar(AFilename), GENERIC_READ, FILE_SHARE_READ, nil,
  18. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
  19. );
  20. if hFile <> INVALID_HANDLE_VALUE then
  21. begin
  22. loSize := GetFileSize(hFile, @hiSize);
  23. {File was opened successfully, now map it:}
  24. hFileMap := CreateFileMapping(
  25. hFile, nil, PAGE_READONLY, hiSize, loSize, 'TextForString'
  26. );
  27. if (hFileMap <> 0) then
  28. begin
  29. if (GetLastError() = ERROR_ALREADY_EXISTS) then
  30. begin
  31. MessageDlg(
  32. 'Mapping already exists - not created.', mtWarning, [mbOk], 0
  33. );
  34. CloseHandle(hFileMap)
  35. end
  36. else
  37. begin
  38. try
  39. {File mapped successfully, now map a view of the file into the
  40. address space:}
  41. view := MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 0);
  42. if (view <> nil) then
  43. begin {View mapped successfully}
  44. {Close file handle - as long is view is open it will persist}
  45. CloseHandle(hFile);
  46. SetLength(Result, loSize);
  47. Move(view^, Result[1], loSize);
  48. end
  49. else
  50. MessageDlg(
  51. 'Unable to map view of file. ' + SysErrorMessage(GetLastError),
  52. mtWarning, [mbOk], 0
  53. );
  54. finally
  55. UnmapViewOfFile(view); {Close view}
  56. CloseHandle(hFileMap); {Close mapping}
  57. end
  58. end
  59. end
  60. else
  61. begin
  62. MessageDlg(
  63. 'Unable to create file mapping. ' + SysErrorMessage(GetLastError),
  64. mtWarning, [mbOk], 0
  65. );
  66. end;
  67. end
  68. else
  69. begin
  70. MessageDlg(
  71. 'Unable to open file. ' + SysErrorMessage(GetLastError),
  72. mtWarning, [mbOk], 0
  73. );
  74. end;
  75. end;

How to read very large text files fast的更多相关文章

  1. Writing Text Files On The Client in Oracle Forms 10g

    Below is the example to write file on client in Oracle Forms 10g with webutil library package.Note:  ...

  2. Access text files using SQL statements by DB Query Analyzer

    Access text files using SQL statements by DB Query Analyzer Ma Gen feng (Guangdong Unitoll Services ...

  3. LaF: Fast Access to Large ASCII Files

    貌似可以随机读取dataframe格式的文本文件.

  4. tomcat gzip compression not working for large js files

    solution 1: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout=&quo ...

  5. How to remove duplicate lines in a large text file?

    How would you remove duplicate lines from a file that is  much too large to fit in memory? The dupli ...

  6. text files and binary files

    https://en.wikipedia.org/wiki/Text_file https://zh.wikipedia.org/wiki/文本文件

  7. interleave two text files with specified lines

    a_file=$1 a_step=$2 b_file=$3 b_step=$4 a_start=1 let a_end=$a_start+$a_step b_start=1 let b_end=$b_ ...

  8. Convert between Unix and Windows text files - IU Knowledge Base from: https://kb.iu.edu/d/acux

    vi. To input the ^M character, press Ctrl-v , and then press Enter or return . In vim, use :set ff=u ...

  9. GIT之二 基础篇(1)

    GIT基础 取得项目的 Git 仓库 有两种取得 Git 项目仓库的方法.第一种是在现存的目录下,通过导入所有文件来创建新的 Git 仓库.第二种是从已有的 Git 仓库克隆出一个新的镜像仓库来. 在 ...

随机推荐

  1. bzoj 3572 [Hnoi2014]世界树——虚树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3572 关于虚树:https://www.cnblogs.com/zzqsblog/p/556 ...

  2. POJ2559最大矩形面积——单调栈

    题目:http://poj.org/problem?id=2559 #include<iostream> #include<cstdio> using namespace st ...

  3. REST风格框架:从MVC到前后端分离***

    摘要: 本人在前辈<从MVC到前后端分离(REST-个人也认为是目前比较流行和比较好的方式)>一文的基础上,实现了一个基于Spring的符合REST风格的完整Demo,具有MVC分层结构并 ...

  4. serialize unserialize

    转自 http://www.cnblogs.com/yeer/archive/2009/03/25/1421161.html php函数serialize()与unserialize()   seri ...

  5. 【Oracle学习笔记-1】Win7下安装Oracle 10g

    源程序获取 从Oracle的官网上下载Oracle 10g: 10203_vista_w2k8_x86_production_db.zip:==>服务器(必须) 10203_vista_w2k8 ...

  6. bzoj1050 旅行

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求一条路径,使得路径上最大 ...

  7. HDU 1116 Play on Words(并查集和欧拉回路)(有向图的欧拉回路)

    Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. COMMON INTERVIEW QUESTIONS

    1. What do you see yourself doing five years from now? 2. What motivates you to put forth your great ...

  9. Mac parallels desktop安装windows,linux

    前言 这款软件你就看作是虚拟机vm,如果你要安装win10系统,请下载ios镜像文件 下载准备工作 Parallels Desktop 13 破解版本 联系站长所要 win10 iso镜像文件    ...

  10. C#后台调用前台javascript的五种方法小结

    第一种,OnClientClick (vs2003不支持这个方法) <asp:Button ID="Button1" runat="server" Tex ...