[fpc-pascal] File handling: the TFileStream or the classical way is faster and more efficient?

Géza Kovacs Géza kg0231 at gmail.com
Mon May 25 06:42:39 CEST 2015


Hi All!

What is the faster and more efficient, using the TFileStream or the
classical way ("file of byte", or "file") type with
blockread/blockwrite and the other well-know procedures?
What is the better, faster on large files?

See the two example code below.

Program FileCopy_stream;
{$mode objfpc} {$H+}
uses classes,SysUtils;
var
	BytesRead,TotalBytesRead : Int64;
	puffer : array [1..1048576] of byte;
	InF, OutF : TFileStream;
begin
		TotalBytesRead := 0;
		BytesRead := 0;
		try
			InF:= TFileStream.Create(ParamSTR(1),fmOpenRead);
			OutF := TFileStream.Create(ParamSTR(2),fmCreate);
				repeat
					BytesRead := InF.Read(puffer,sizeof(puffer));
					inc(TotalBytesRead, BytesRead);
					OutF.Write(puffer,BytesRead);
				until (TotalBytesRead = InF.size);
			finally
			FreeAndNil(InF);
			FreeAndNil(OutF);
		end;
end.

Program FileCopy_Classic;
{$mode objfpc} {$H+}
var
	NumRead, NumWritten : LongInt;
	puffer : array [1..1048576] of byte;
	InF,OutF : file of byte;
begin
		assign(InF,ParamStr(1));
		ReSet(InF);
		Assign(OutF,ParamStr(2));
		ReWrite(OutF);
		repeat
			BlockRead(InF,puffer,SizeOf(puffer),NumRead);
			BlockWrite(OutF,puffer,NumRead,NumWritten);
		until (NumRead = 0);
		close(InF);
		close(OutF);
end.



More information about the fpc-pascal mailing list