如何从缓存中卸载文件? [英] How to unload a file from cache?
问题描述
有人知道如何从缓存中卸载文件吗?
我将文件写入磁盘,然后想将其读回。但是,Windows从缓存中给了我文件。
Anybody knows how to unload a file from cache? I write a file to disk, then I want to read it back. However, Windows is giving me the file from cache.
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;
-
我正在尝试确定驱动器的写入/读取速度。
- I am trying to determine the write/read speed of a drive.
推荐答案
一些代码演示 FILE_FLAG_NO_BUFFERING
的用法并测试其影响您的阅读时间:
Some code to demonstrate the use of FILE_FLAG_NO_BUFFERING
and to test how it affects your reading time:
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的文件运行此代码会在我的系统上显示:
Running this code with a file of 420 MByte size gives on my system:
缓冲文件读取时间:3,974 s
缓冲文件读取时间:0,922 s
缓冲文件读取时间读取:0,937 s
缓冲文件读取时间:0,937 s
读取缓冲文件的时间:0,938 s
非缓冲文件读取的时间:3,922 s < br>
读取非缓冲文件的时间:4,000 s
读取非缓冲文件的时间:4,016 s
读取非缓冲文件的时间:4,062 s
读取无缓冲文件的时间:3,985 s
Time for buffered file read: 3,974 s
Time for buffered file read: 0,922 s
Time for buffered file read: 0,937 s
Time for buffered file read: 0,937 s
Time for buffered file read: 0,938 s
Time for unbuffered file read: 3,922 s
Time for unbuffered file read: 4,000 s
Time for unbuffered file read: 4,016 s
Time for unbuffered file read: 4,062 s
Time for unbuffered file read: 3,985 s
这篇关于如何从缓存中卸载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!