procedure BenchmarkFileStream(AFile: string);
  var
    I, Position, PackageCount: cardinal;
    FS: TFileStream;
    Buffer: byte;
    T0: double;
  begin
    T0 := Now;
    Buffer := 0;
    try
      FS := TFileStream.Create(AFile, fmOpenRead);
      PackageCount := FS.Size div 188;

      if PackageCount > 0 then
        for I := 0 to PackageCount - 1 do
        begin
          try
            FS.Position := I * 188;
            FS.ReadBuffer(Buffer, 1);            
          except
            on E: Exception do
              WriteLn('Exception: ' + E.Message);
          end;
        end
    finally
      FS.Free;
    end;
    WriteLn('FileStream Time: ' + IntToStr(MilliSecondsBetween(T0, Now)));
  end;

  procedure BenchmarkBufferIO(AFile: string);
  var
    I, Position, PackageCount: cardinal;
    Buffer: byte;
    T0: double;
    MyFile: file;
    NumberRead: word;
  begin
    T0 := Now;
    Buffer := 0;
    try
      AssignFile(MyFile, AFile);
      Reset(MyFile, 1);
      PackageCount := FileSize(MyFile) div 188;

      if PackageCount > 0 then
        for I := 0 to PackageCount - 1 do
        begin
          try
            Seek(MyFile, I * 188);
            BlockRead(MyFile, Buffer, SizeOf(Buffer), NumberRead);            
          except
            on E: Exception do
              WriteLn('Exception: ' + E.Message);
          end;
        end
    finally
      Close(MyFile);
    end;
    WriteLn('BufferIO Time: ' + IntToStr(MilliSecondsBetween(T0, Now)));
  end;

  procedure BenchmarkFileHandle(AFile: string);
  var
    FileHandle: integer;
    I, PackageCount: cardinal;
    Buffer: byte;
    T0: double;
    MyFile: file;
  begin
    T0 := Now;
    Buffer := 0;
    try
      AssignFile(MyFile, AFile);
      Reset(MyFile, 1);
      PackageCount := FileSize(MyFile) div 188;
      Close(MyFile);

      FileHandle := FileOpen(AFile, fmOpenRead);
      Filemode := fmOpenRead;

      if PackAgeCount > 0 then
        for I := 0 to PackageCount - 1 do
        begin
          try
            FileSeek(FileHandle, I * 188, 0);
            FileRead(FileHandle, Buffer, SizeOf(Buffer));            
          except
            on E: Exception do
              WriteLn('Exception: ' + E.Message);
          end;
        end
    finally
      FileClose(FileHandle);
    end;
    WriteLn('FileHandle Time: ' + IntToStr(MilliSecondsBetween(T0, Now)));
  end;

全站熱搜

bagatelles 發表在 痞客邦 留言(0) 人氣()