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;

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. c# list排序的三种实现方式 (转帖)

    用了一段时间的gridview,对gridview实现的排序功能比较好奇,而且利用C#自带的排序方法只能对某一个字段进行排序,今天demo了一下,总结了三种对list排序的方法,并实现动态传递字段名对 ...

  2. Angular 4.0 环境搭建

    1.安装node 2.angular cli安装 sudo npm install -g @angular/cli 3. 使用ng -v 查看安装结果 4. 创建项目 ng new helloworl ...

  3. 从windows到linux的shell脚本编码和格式问题

    从windows到linux的shell脚本编码和格式问题   从windows到Linux的shell脚本编码和格式问题 1.异常问题 :set ff=unix 启动脚本在启动时报错比如执行sh s ...

  4. 添加mysamba

    一. 复制/home/tingpan/openwrt/barrier_breaker/feeds/luci/applications文件夹下的luci-samba文件,将文件中的内容samba改为my ...

  5. linux 添加secondary ip

    linux下ip地址除了primary外,还有两种:1. ip alias(子接口)2. secondary ip(辅助ip) 都可在一块物理网卡上添加,alias由ifconfig添加,ifconf ...

  6. [UE4]动态数组:TArray容器

    为什么使用UE4提供的容器类? 如果你用过C++的STL库,你就知道STL提供了各种各样的容器/数据结构,使得你对处理很多数据的时候非常快捷高效.UE4同样也提供了类似的库,库里面的类型是以T开头的, ...

  7. [datatable]借助DataTable的Compute方法

    借助DataTable的Compute方法,DataTable中数据不用事先排好序. 下面代码中的dt是跟前面的是一样的 DataTable dtName = dt.DefaultView.ToTab ...

  8. 【Linux_Unix系统编程】Chapter4 文件IO

    Chapter4 文件IO 4.1 概述 文件描述符 == Windows的句柄 标准文件描述符: 0 标准输入 STDIN_FILENO stdin 1 标准输出 STDOUT_FILENO std ...

  9. 11g RAC 更改归档模式 ,归档文件存放在ASM 磁盘组(转载)

    11g RAC 更改归档模式 ASM 本实验有两个节点tip01,tip02oracle_sid 分别是 tips1,tips2 1.在节点1 tip01上执行[root@tip01 ~]# su - ...

  10. express有中间件的增删改查

    var express = require('express');引入express框架 var router = express.Router();引入router路由级中间件 var data = ...