TFileStream itself does not perform buffering, which is processed by the OS and, as a rule, is sufficient for most purposes.
My suggestion was to create a method to write your data to the stream, and then pass the TSTream parameter to this method. Thus, you can easily test various options without affecting your program.
For instance:
Procedure TForm1.StreamMyObjects(aStream : tStream); begin aStream.Write( MyString[1], Length( MyString ) * SizeOf( Char )); aStream.Write( CRLF, Length( CRLF ) * SizeOf( Char )); aStream.Write( MyOtherString[1], Length( MyOtherString ) * SizeOf( Char )); aStream.Write( CRLF, Length( CRLF ) * SizeOf( Char )); end;
In JCL, as mentioned earlier, there is a TJclBufferedStream, which you can then check to see if there is any performance advantage that will depend on what you write and how much you write. For example, the following will check TFileStream and tJCLBufferedStream to find out what the differences are (yes, I know I miss TRY / FINALLY):
var fstm : tFileSTream; fBufStm : tJCLBufferedStream; iTicks : Cardinal; fModes : word; // for SO formatting. begin fModes := fmOpenReadWrite or fmCreate or fmShareExclusive; iTicks := GetTickCount; fstm := tFilestream.create('test1.txt',fModes); StreamMyObjects( fStm ); fstm.free; ShowMessage('TEST1='+IntToSTr(GetTickCount-iTicks)); iTicks := GetTickCount; fstm := tFilestream.create('test2.txt',fModes); fBufStm := tJclBufferedStream.create( fStm ); StreamMyObjects( fBufStm ); fBufStm.free; fstm.free; ShowMessage('TEST2='+IntToSTr(GetTickCount-iTicks)); end;
in my test, the following procedure:
procedure TForm1.StreamMyObjects(aSTream: tStream); var St : string; ix : integer; begin for ix := 0 to 10000 do begin St := 'This is a string which is written to a stream. ' + IntToStr(ix); aStream.Write(st[1], Length(st) * SizeOf(Char) ); end; end;
returned 47 for tFilestream and 16 for tJCLBufferedStream. Without a loop, time is short, so you need to test your data ... and how much you write.