[fpc-pascal] File handling: the TFileStream or the classical way is faster and more efficient?
Michael Van Canneyt
michael at freepascal.org
Mon May 25 11:03:04 CEST 2015
On Mon, 25 May 2015, Géza Kovacs Géza wrote:
> 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?
TFileStream offers more flexibility, but as far as efficiency is concerned :
I think that will be the same for both approaches.
For example, your first example can be done easier in a single statement:
OutF.CopyFrom(Inf,0);
Michael.
>
> 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.
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
More information about the fpc-pascal
mailing list