gpt4 book ai didi

delphi - 如何从缓存中卸载文件?

转载 作者:行者123 更新时间:2023-12-03 18:54:25 26 4
gpt4 key购买 nike

有人知道如何从缓存中卸载文件吗?
我将文件写入磁盘,然后想将其读回。但是,Windows从缓存中给了我文件。

begin
...

{-- Write file --}
AssignFile(F, FileName);
Rewrite(F, 1);
BlockWrite(F, Buf[0], Chunk);
CloseFile(F); { FLUSH }

some code...
then.....

{-- Read file --}
AssignFile(F, FileName);
Reset(F, 1);
BlockRead(F, Buf[0], Chunk); <----------- getting file from cache
CloseFile(F);
end;


--
我试图确定驱动器的写入/读取速度。

最佳答案

一些代码演示FILE_FLAG_NO_BUFFERING的用法并测试它如何影响您的阅读时间:

uses
MMSystem;

function GetTimeForRead(ABuffered: boolean): single;
const
FileToRead = // name of file with maybe 500 MByte size
var
FlagsAndAttributes: DWORD;
FileHandle: THandle;
SrcStream, DestStream: TStream;
Ticks: DWord;
begin
if ABuffered then
FlagsAndAttributes := FILE_ATTRIBUTE_NORMAL
else
FlagsAndAttributes := FILE_FLAG_NO_BUFFERING;
FileHandle := CreateFile(FileToRead, GENERIC_READ, FILE_SHARE_READ, nil,
OPEN_EXISTING, FlagsAndAttributes, 0);
if FileHandle = INVALID_HANDLE_VALUE then begin
Result := 0.0;
exit;
end;

SrcStream := THandleStream.Create(FileHandle);
try
DestStream := TMemoryStream.Create;
try
DestStream.Size := SrcStream.Size;

Sleep(0);
Ticks := timeGetTime;
DestStream.CopyFrom(SrcStream, SrcStream.Size);
Result := 0.001 * (timeGetTime - Ticks);

finally
DestStream.Free;
end;
finally
SrcStream.Free;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
Button1.Enabled := FALSE;
try
Update;
Memo1.Lines.Clear;
for i := 1 to 5 do begin
Memo1.Lines.Add(Format('Time for buffered file read: %.3f s',
[GetTimeForRead(TRUE)]));
end;
for i := 1 to 5 do begin
Memo1.Lines.Add(Format('Time for unbuffered file read: %.3f s',
[GetTimeForRead(FALSE)]));
end;
finally
Button1.Enabled := TRUE;
end;
end;


使用420 MB的文件运行此代码可以在我的系统上运行:


读取缓冲文件的时间:3,974 s
读取缓冲文件的时间:0,922 s
读取缓冲文件的时间:0,937 s
读取缓冲文件的时间:0,937 s
读取缓冲文件的时间:0,938 s
读取非缓冲文件的时间:3,922 s
无缓冲文件读取时间:4,000 s
读取非缓冲文件的时间:4,016 s
读取非缓冲文件的时间:4,062 s
读取无缓冲文件的时间:3,985 s

关于delphi - 如何从缓存中卸载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/983931/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com