Конференция "KOL" » KOL PStream медленне чем TFileStream [Delphi]
 
  • alexk47 (18.06.13 16:27) [0]
    Всем Доброго времени суток.
    Пытаюсь сделать base64 конвертор для своего проекта, нашел вот такие исходники http://www.yunqa.de/delphi/doku.php/products/mime/index, переписал основной класс(DIMimeStreams) под KOL с использованием PStream(NewReadFileStream/NewWriteFileStream) и он оказался медленным.

    подскажите что я делаю не так


    unit DIMimeStreamsKOL;

    {$I DICompilers.inc}

    interface

    uses
     KOL;

    procedure MimeEncodeStream(
     const InputStream: PStream;
     const OutputStream: PStream);

    procedure MimeEncodeStreamNoCRLF(
     const InputStream: PStream;
     const OutputStream: PStream);

    procedure MimeDecodeStream(
     const InputStream: PStream;
     const OutputStream: PStream);

    procedure MimeEncodeFile(
     const InputFileName: string;
     const OutputFileName: string);

    procedure MimeEncodeFileNoCRLF(
     const InputFileName: string;
     const OutputFileName: string);

    procedure MimeDecodeFile(
     const InputFileName: string;
     const OutputFileName: string);

    implementation

    uses
     DISystemCompat,
     DIMime;

    const

     MIME_BUFFER_SIZE = MIME_DECODED_LINE_BREAK * 3 * 4 * 4;

    procedure MimeEncodeStream(
     const InputStream: PStream;
     const OutputStream: PStream);
    var
     InputBuffer: packed array[0..MIME_BUFFER_SIZE - 1] of Byte;
     OutputBuffer: packed array[0..(MIME_BUFFER_SIZE + 2) div 3 * 4 + MIME_BUFFER_SIZE div MIME_DECODED_LINE_BREAK * 2 - 1] of Byte;
     BytesRead: NativeUInt;
     iDelta, ODelta: NativeUInt;
    begin
     BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));

     while BytesRead = SizeOf(InputBuffer) do
       begin
         MimeEncodeFullLines(InputBuffer, SizeOf(InputBuffer), OutputBuffer);
         OutputStream.Write(OutputBuffer, SizeOf(OutputBuffer));
         BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
       end;

     MimeEncodeFullLines(InputBuffer, BytesRead, OutputBuffer);

     iDelta := BytesRead div MIME_DECODED_LINE_BREAK;
     ODelta := iDelta * (MIME_ENCODED_LINE_BREAK + 2);
     iDelta := iDelta * MIME_DECODED_LINE_BREAK;
     MimeEncodeNoCRLF((PAnsiChar(@InputBuffer) + iDelta)^, BytesRead - iDelta, (PAnsiChar(@OutputBuffer) + ODelta)^);

     OutputStream.Write(OutputBuffer, MimeEncodedSize(BytesRead));
    end;

    procedure MimeEncodeStreamNoCRLF(
     const InputStream: PStream;
     const OutputStream: PStream);
    var
     InputBuffer: packed array[0..MIME_BUFFER_SIZE - 1] of Byte;
     OutputBuffer: packed array[0..((MIME_BUFFER_SIZE + 2) div 3) * 4 - 1] of Byte;
     BytesRead: NativeUInt;
    begin
     BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
     while BytesRead = SizeOf(InputBuffer) do
       begin
         MimeEncodeNoCRLF(InputBuffer, SizeOf(InputBuffer), OutputBuffer);
         OutputStream.Write(OutputBuffer, SizeOf(OutputBuffer));
         BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
       end;

     MimeEncodeNoCRLF(InputBuffer, BytesRead, OutputBuffer);
     OutputStream.Write(OutputBuffer, MimeEncodedSizeNoCRLF(BytesRead));
    end;

    procedure MimeDecodeStream(
     const InputStream: PStream;
     const OutputStream: PStream);
    var
     ByteBuffer, ByteBufferSpace: NativeUInt;
     InputBuffer: packed array[0..MIME_BUFFER_SIZE - 1] of Byte;
     OutputBuffer: packed array[0..(MIME_BUFFER_SIZE + 3) div 4 * 3 - 1] of Byte;
     BytesRead: NativeUInt;
    begin
     ByteBuffer := 0;
     ByteBufferSpace := 4;
     BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
     while BytesRead > 0 do
       begin
         OutputStream.Write(OutputBuffer, MimeDecodePartial(InputBuffer, BytesRead, OutputBuffer, ByteBuffer, ByteBufferSpace));
         BytesRead := InputStream.Read(InputBuffer, SizeOf(InputBuffer));
       end;
     OutputStream.Write(OutputBuffer, MimeDecodePartialEnd(OutputBuffer, ByteBuffer, ByteBufferSpace));
    end;

    procedure MimeEncodeFile(
     const InputFileName: string;
     const OutputFileName: string);
    var
     InputStream, OutputStream: PStream;
    begin
     InputStream := NewReadFileStream(InputFileName);
     try
       OutputStream := NewWriteFileStream(OutputFileName);
       try
         MimeEncodeStream(InputStream, OutputStream);
       finally
         OutputStream.Free;
       end;
     finally
       InputStream.Free;
     end;
    end;

    procedure MimeEncodeFileNoCRLF(
     const InputFileName: string;
     const OutputFileName: string);
    var
     InputStream, OutputStream: PStream;
    begin
     InputStream := NewReadFileStream(InputFileName);
     try
       OutputStream := NewWriteFileStream(OutputFileName);
       try
         MimeEncodeStreamNoCRLF(InputStream, OutputStream);
       finally
         OutputStream.Free;
       end;
     finally
       InputStream.Free;
     end;
    end;

    procedure MimeDecodeFile(
     const InputFileName: string;
     const OutputFileName: string);
    var
     InputStream, OutputStream: PStream;
    begin
     InputStream := NewReadFileStream(InputFileName);
     try
       OutputStream := NewWriteFileStream(OutputFileName);
       try
         MimeDecodeStream(InputStream, OutputStream);
       finally
         OutputStream.Free;
       end;
     finally
       InputStream.Free;
     end;
    end;

    end.

  • NoUser © (18.06.13 17:03) [1]
    Прочитайте все данные из файла за один read и запишите за один write.
    В прмежутке используйте NewExMemoryStream.
  • Thaddy © (24.06.13 14:02) [2]
    You can try the buffered stream from http://thaddy.co.uk/kolstreams.pas
    This is much faster.
 
Конференция "KOL" » KOL PStream медленне чем TFileStream [Delphi]
Есть новые Нет новых   [134427   +34][b:0][p:0.003]