緩沖文件(用于更快的磁盤(pán)訪問(wèn))我正在處理大文件,直接寫(xiě)入磁盤(pán)很慢。因?yàn)槲募艽?,我無(wú)法在TMemoryStream中加載它。TFileStream沒(méi)有緩沖,所以我想知道是否有一個(gè)可以提供緩沖流的自定義庫(kù),或者我是否應(yīng)該只依賴(lài)OS提供的緩沖。OS緩沖是否可靠?我的意思是如果緩存已滿,可以從緩存中刷新舊文件(我的)以便為新文件騰出空間。我的文件在GB范圍內(nèi)。它包含數(shù)百萬(wàn)條記錄。不幸的是,記錄不是固定大小。所以,我必須做數(shù)百萬(wàn)的讀數(shù)(4到500字節(jié)之間)。閱讀(和寫(xiě)作)是順序的。我不會(huì)上下跳進(jìn)文件(我認(rèn)為這是緩沖的理想選擇)。最后,我必須將這樣的文件寫(xiě)回磁盤(pán)(再次寫(xiě)入數(shù)百萬(wàn)的小寫(xiě))。對(duì)David Heffernan贊不絕口!David提供了一段很棒的代碼,提供緩沖磁盤(pán)訪問(wèn)。人們你必須擁有他的BufferedFileStream!這是黃金。并且不要忘記upvote。謝謝大衛(wèi)。 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.
______
緩沖文件(用于更快的磁盤(pán)訪問(wèn))
寶慕林4294392
2019-08-26 15:29:27