Wednesday, February 10, 2010

String Buffer Performance

Benchmarks for the yesterday's post.

 
program project1;
 
{$mode objfpc}{$H+}
 
uses
  Classes, DateUtils, SysUtils, StrBuf;
 
type
  TBench = procedure(const PartSize, MaxSize: Integer);
 
procedure BuildString(const PartSize, MaxSize: Integer);
var
  Buf: String;
  BufSize: Integer;
begin
  Buf := '';
  BufSize := 0;
  while BufSize < MaxSize do begin
    Buf += StringOfChar(' ', PartSize);
    BufSize += PartSize;
  end;
end;
 
procedure BuildStringList(const PartSize, MaxSize: Integer);
var
  Buf: TStringList;
  BufSize: Integer;
begin
  Buf := TStringList.Create;
  BufSize := 0;
  while BufSize < MaxSize do begin
    Buf.Add(StringOfChar(' ', PartSize));
    BufSize += PartSize;
  end;
  Buf.Free;
end;
 
procedure BuildStrBuf(const PartSize, MaxSize: Integer);
var
  Buf: TStrBuf;
  BufSize: Integer;
begin
  BufSize := 0;
  while BufSize < MaxSize do begin
    Buf.W(StringOfChar(' ', PartSize));
    BufSize += PartSize;
  end;
end;
 
procedure Build(const PartSize, MaxSize: Integer; Bench: TBench; const Name: String);
const RUN = 5;
var
  T0: TDateTime;
  Msec: Int64;
  I: Integer;
begin
  T0 := Now;
  for I := 1 to RUN do
    Bench(PartSize, MaxSize);
  MSec := MilliSecondsBetween(Now, T0);
  Writeln(Format('partsize: %.5d T: %.7dms %s ', [PartSize, MSec, Name]));
end;
 
const
  UBOUND_PART = 100;
  UBOUND_BUF = 1000000;
var
  PartSize: Integer;
begin
  PartSize := 1;
  repeat
    Build(PartSize, UBOUND_BUF, @BuildString, 'String');
    Build(PartSize, UBOUND_BUF, @BuildStringList, 'StringList');
    Build(PartSize, UBOUND_BUF, @BuildStrBuf, 'StrBuf');
    PartSize := ((3 * PartSize) + 1) div 2;
  until PartSize > UBOUND_PART;
end.

$ fpc -B ./project1 && ./project1 | sort
Free Pascal Compiler version 2.5.1 [2010/02/07] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling ./project1.pas
Compiling strbuf.pas
Linking project1
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
189 lines compiled, 0.2 sec 
partsize: 00001 T: 0000446ms StrBuf 
partsize: 00001 T: 0000602ms String 
partsize: 00001 T: 0001150ms StringList 
partsize: 00002 T: 0000233ms StrBuf 
partsize: 00002 T: 0000261ms String 
partsize: 00002 T: 0000534ms StringList 
partsize: 00003 T: 0000159ms StrBuf 
partsize: 00003 T: 0000190ms String 
partsize: 00003 T: 0000379ms StringList 
partsize: 00005 T: 0000101ms StrBuf 
partsize: 00005 T: 0000127ms String 
partsize: 00005 T: 0000211ms StringList 
partsize: 00008 T: 0000076ms StrBuf 
partsize: 00008 T: 0000086ms String 
partsize: 00008 T: 0000167ms StringList 
partsize: 00012 T: 0000050ms StrBuf 
partsize: 00012 T: 0000060ms String 
partsize: 00012 T: 0000100ms StringList 
partsize: 00018 T: 0000038ms StrBuf 
partsize: 00018 T: 0000043ms String 
partsize: 00018 T: 0000069ms StringList 
partsize: 00027 T: 0000030ms StrBuf 
partsize: 00027 T: 0000033ms String 
partsize: 00027 T: 0000045ms StringList 
partsize: 00041 T: 0000023ms String 
partsize: 00041 T: 0000024ms StrBuf 
partsize: 00041 T: 0000034ms StringList 
partsize: 00062 T: 0000015ms StrBuf 
partsize: 00062 T: 0000020ms String 
partsize: 00062 T: 0000020ms StringList 
partsize: 00093 T: 0000014ms String 
partsize: 00093 T: 0000015ms StringList 
partsize: 00093 T: 0000017ms StrBuf 
$

StrBuf's performance looks good or better compared to TStringList and/or String concatenating while you're appending short pieces of text, say up to few dozens of characters and that's what it was intended and written for.

Tuesday, February 9, 2010

Simple String Buffer

 
unit StrBuf;
 
{$mode objfpc}{$H+}
 
{ Simple string buffer
 
  No setup/teardown required, memory mngmt, initialization, and finalization
  is done by FPC itself.
 
  Copyright (C) 2010 bflm, contact: befelemepeseveze at Google's free mail
 
  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version with the following modification:
 
  As a special exception, the copyright holders of this library give you
  permission to link this library with independent modules to produce an
  executable, regardless of the license terms of these independent modules,and
  to copy and distribute the resulting executable under terms of your choice,
  provided that you also meet, for each linked independent module, the terms
  and conditions of the license of that module. An independent module is a
  module which is not derived from or based on this library. If you modify
  this library, you may extend this exception to your version of the library,
  but you are not obligated to do so. If you do not wish to do so, delete this
  exception statement from your version.
 
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  for more details.
 
  You should have received a copy of the GNU Library General Public License
  along with this library; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
 
interface
 
uses
  SysUtils;
 
type
 
  { TStrBuf }
 
  TStrBuf = object
  private
    FLen: Integer;
    FS: String;
    function GetS: String;
  public
    procedure W(const S: String);
    procedure W(const Fmt: String; Args: array of const);
    property S: String read GetS;
  end;
 
operator +(var Buf: TStrBuf; const S: String): TStrBuf;
operator :=(const S: String): TStrBuf;
operator :=(var Buf: TStrBuf): String;
 
implementation
 
{ TStrBuf }
 
function TStrBuf.GetS: String;
begin
  if FS = '' then
    FLen := 0;
  Result := LeftStr(FS, FLen);
end;
 
procedure TStrBuf.W(const S: String);
var Len, Len1: Integer;
begin
  Len := Length(S);
  if Len = 0 then
    Exit;
  if FS = '' then
    FLen := 0;
  Len1 := FLen + Len;
  if Len1 > Length(FS) then
    SetLength(FS, 2 * Len1);
  Move(S[1], FS[FLen + 1], Len);
  FLen += Len;
end;
 
procedure TStrBuf.W(const Fmt: String; Args: array of const);
begin
  W(Format(Fmt, Args));
end;
 
operator +(var Buf: TStrBuf; const S: String): TStrBuf;
begin
  Result.FS := Buf.FS;
  Result.FLen := Buf.FLen;
  Result.W(S);
end;
 
operator :=(const S: String): TStrBuf;
begin
  Result.FLen := Length(S);
  Result.FS := S;
end;
 
operator :=(var Buf: TStrBuf): String;
begin
  Result := Buf.S;
end;
 
end.
 

 
program project1;
 
{$mode objfpc}{$H+}
 
// Simple string buffer example
 
uses
  heaptrc, StrBuf;
 
function F(N: Integer): String;
var
  Buf: TStrBuf;
  I: Integer;
begin
  for I := 1 to N do
    Buf.W('line %d%s', [I, sLineBreak]);
  Result := Buf.S;
end;
 
var
  Buf, Buf2: TStrBuf;
 
begin
  Buf.W('abc');
  Buf.W('%d', [123]);
  Buf += 'xyz';
  Writeln(Buf.S);
  Writeln(String(Buf));
  Buf := '456';
  Writeln(Buf.S);
  Buf2 := Buf + ' bar';
  Buf += ' foo';
  Writeln(Buf.S);
  Writeln(Buf2.S);
  Write(F(10));
end.

$ fpc -B project1.pas && ./project1 
Free Pascal Compiler version 2.5.1 [2010/02/07] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling project1.pas
Compiling strbuf.pas
Linking project1
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
148 lines compiled, 0.2 sec 
abc123xyz
abc123xyz
456
456 foo
456 bar
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
Heap dump by heaptrc unit
71 memory blocks allocated : 2926/3200
71 memory blocks freed     : 2926/3200
0 unfreed memory blocks : 0
True heap size : 360448
True free heap : 360448
$