Я работаю с большими файлами, и запись напрямую на диск идет медленно. Поскольку файл большой, я не могу загрузить его в TMemoryStream.
TFileStream не буферизуется, поэтому я хочу знать, есть ли специальная библиотека, которая может предлагать буферизованные потоки, или я должен полагаться только на буферизацию, предлагаемую ОС. Надежна ли буферизация ОС? Я имею в виду, если кэш заполнен, старый файл (мой) может быть удален из кэша, чтобы освободить место для нового файла.
Мой файл находится в диапазоне ГБ. Он содержит миллионы записей. К сожалению, записи не имеют фиксированного размера. Итак, я должен сделать миллионы чтений (от 4 до 500 байтов). Чтение (и письмо) является последовательным. Я не прыгаю вверх и вниз в файл (который я считаю идеальным для буферизации).
В конце концов, я должен записать такой файл обратно на диск (снова миллионы небольших записей).
Слово похвалы за Дэвида Хеффернана!
Дэвид предоставил БОЛЬШОЙ кусок кода, который обеспечивает буферизованный доступ к диску.
ЛЮДИ, КОТОРЫЕ ВЫ ДОЛЖНЫ ИМЕТЬ ЕГО BufferedFileStream! Это золото. И не забудьте поднять голос.
Спасибо Дэвид.
Speed tests:
Input file: 317MB.SFF
Delphi stream: 9.84sec
David stream: 2.05sec
______________________________________
More tests:
Input file: input2_700MB.txt
(PS: multiply time with 10)
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.
______
Reading: Random (ReadInteger) (100000 reads)
SSD
Time: 064 ms. Cache size: 1KB. Count: 100000. RAM: 13.27 MB <-- probably the best buffer size for ReadInteger is 4bytes!
Time: 067 ms. Cache size: 2KB. Count: 100000. RAM: 13.27 MB
Time: 080 ms. Cache size: 4KB. Count: 100000. RAM: 13.27 MB
Time: 098 ms. Cache size: 8KB. Count: 100000. RAM: 13.27 MB
Time: 140 ms. Cache size: 16KB. Count: 100000. RAM: 13.27 MB
Time: 213 ms. Cache size: 32KB. Count: 100000. RAM: 13.27 MB
Time: 360 ms. Cache size: 64KB. Count: 100000. RAM: 13.27 MB
Conclusion: don't use it for "random" reading