为了账号安全,请及时绑定邮箱和手机立即绑定

缓冲文件(用于更快的磁盘访问)

缓冲文件(用于更快的磁盘访问)

缓冲文件(用于更快的磁盘访问)我正在处理大文件,直接写入磁盘很慢。因为文件很大,我无法在TMemoryStream中加载它。TFileStream没有缓冲,所以我想知道是否有一个可以提供缓冲流的自定义库,或者我是否应该只依赖OS提供的缓冲。OS缓冲是否可靠?我的意思是如果缓存已满,可以从缓存中刷新旧文件(我的)以便为新文件腾出空间。我的文件在GB范围内。它包含数百万条记录。不幸的是,记录不是固定大小。所以,我必须做数百万的读数(4到500字节之间)。阅读(和写作)是顺序的。我不会上下跳进文件(我认为这是缓冲的理想选择)。最后,我必须将这样的文件写回磁盘(再次写入数百万的小写)。对David Heffernan赞不绝口!David提供了一段很棒的代码,提供缓冲磁盘访问。人们你必须拥有他的BufferedFileStream!这是黄金。并且不要忘记upvote。谢谢大卫。   Speed tests:      Input file: 317MB.SFF      Delphi stream: 9.84sec      David's stream: 2.05sec     ______________________________________    More tests:      Input file: input2_700MB.txt      Lines: 19 millions      Compiler optimization: ON      I/O check: On      FastMM: release mode     **HDD**         Reading: **linear** (ReadLine) (PS: multiply time with 10)             We see clear performance drop at 8KB. Recommended 16 or 32KB         Time: 618 ms  Cache size: 64KB.         Time: 622 ms  Cache size: 128KB.         Time: 622 ms  Cache size: 24KB.         Time: 622 ms  Cache size: 32KB.         Time: 622 ms  Cache size: 64KB.         Time: 624 ms  Cache size: 256KB.         Time: 625 ms  Cache size: 18KB.         Time: 626 ms  Cache size: 26KB.         Time: 626 ms  Cache size: 1024KB.         Time: 626 ms  Cache size: 16KB.         Time: 628 ms  Cache size: 42KB.         Time: 644 ms  Cache size: 8KB.      <--- no difference until 8K         Time: 664 ms  Cache size: 4KB.         Time: 705 ms  Cache size: 2KB.         Time: 791 ms  Cache size: 1KB.         Time: 795 ms  Cache size: 1KB.       **SSD**       We see a small improvement as we go towards higher buffers. Recommended 16 or 32KB         Time: 610 ms  Cache size: 128KB.         Time: 611 ms  Cache size: 256KB.         Time: 614 ms  Cache size: 32KB.         Time: 623 ms  Cache size: 16KB.         Time: 625 ms  Cache size: 66KB.         Time: 639 ms  Cache size: 8KB.       <--- definitively not good with 8K         Time: 660 ms  Cache size: 4KB.      ______
查看完整描述

3 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

TFileStream类内部使用CreateFile,它总是使用缓冲区来管理文件,除非你指定的功能FILE_FLAG_NO_BUFFERING标志(注意这直接使用TFileStream的,你不能指定该标志)。有关更多信息,您可以查看这些链接

您也可以尝试使用Primoz Gabrijelcic TGpHugeFileStreamGpHugeFile单元。


查看完整回答
反对 回复 2019-08-26
  • 3 回答
  • 0 关注
  • 588 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信