Delphi BASE64 modification unit EncdDecd

Delphi BASE64 modification unit EncdDecd

EncdDecd.pas two function declaration:

procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);

For Output parameter, if it is TMemoryStream, efficiency is really too bad, the test found that encodes a 5M many files, to more than ten seconds!

But if TStringStream, in 0.2 to 0.3 seconds!

WHY?

Because TMemoryStream constantly call the Write method, continue to allocate memory requirements of Windows! Resulting in performance degradation! The TStringStream and TFileStream is not the problem.

How to do?

May be a one-time allocation to TMemoryStream good memory space. Suppose the number of bytes before coding is X, the number of bytes is encoded (X + 2) div 3 * 4

On amending carriage return line feed, locate the following code:

K IF> 75 the then     
   the begin 
    bufptr [0]: = # 0D $; // carriage 
    BufPtr [1]: = # $ 0A; // newline 
    Inc is an (bufptr, 2); 
    K: = 0; 
   End;

Every 76 characters, it is mandatory carriage return line feed. Comment it out, because this is actually useless. Means to save the modified EncdDecdEx, use it later on.

Before the encoding / decoding of TMemoryStream Output buffer size parameter is set in advance, to avoid a plurality of times to request memory allocation WINDOWS:

uses
  encddecdEx; 
 var
  Input,Output:TMemoryStream;
 begin
  Input:=TMemoryStream.Create;
  try
   Input.LoadFromFile('c:\aaa.txt');
   Output:=TMemoryStream.Create;
   try
    Output.Size:=(Input.Size + 2) div 3 * 4;
    EncodeStream(Input,Output);
   finally
    Output.Free;
   end;
  finally
   Input.Free;
  end;
 end;

  

 

Guess you like

Origin www.cnblogs.com/hnxxcxg/p/11100160.html