After some tweaking TStringBuilder in FastCode is now 2x faster than the stock Builder in SysUtils.

After some tweaking TStringBuilder in FastCode is now 2x faster than the stock Builder in SysUtils.

See: https://github.com/JBontes/FastCode/blob/master/FastStringBuilder.pas

Here's the timing code:

procedure TForm76.Button1Click(Sender: TObject);
var
B: FastStringBuilder.TStringBuilder;
Ticks: Currency;
S: string;
begin
S:= 'hallo';
B:= FastStringBuilder.TStringBuilder.Create;
Ticks:= THiResStopWatch.Sample(procedure() var i: integer; begin
B.Clear;
for i:= 0 to Runs do begin
B.Append(S);
end;
end,100);
Button1.Caption:= Format('%m',[Ticks]) + ' Ticks, ';
end;

procedure TForm76.Button2Click(Sender: TObject);
var
B: System.SysUtils.TStringBuilder;
Ticks: Currency;
S: string;
begin
S:= 'hallo';
B:= System.SysUtils.TStringBuilder.Create;
Ticks:= THiResStopWatch.Sample(procedure var i: integer; begin
B.Clear;
for i:= 0 to Runs do begin
B.Append(S);
end;
end,100);
Button2.Caption:= Format('%m',[Ticks]) + ' Ticks, ';
end;
https://github.com/JBontes/FastCode/blob/master/FastStringBuilder.pas

Comments

  1. Thach Ngo Test results: TStringBuilder (Emba): 43.000 ms, TNDEStringBuilder: 25.000 ms, TStringBuilder (fastcode) 40.000 ms. The difference is entirely due to:
    Builder.Append(const Value: char)
    This routine is called 260.000 times, where as the other routines are only called a 1.000 times. Thanks. I'll improve this routine. Your test does not measure fairly due to this skewed weighing of this one routine.

    ReplyDelete
  2. Update: Append(single char) is now faster than TNDEStringBuilder on x64 and a little slower on x86. Both are about 3-4x faster than before. Every other routine is faster on FastStringBuilder.

    ReplyDelete

Post a Comment