Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: StringBuilderDemo.dproj

Project Structure

StringBuilderDemo.dpr
program StringBuilderDemo;

uses
  Forms,
  StringBuilderDemo_MainForm in 'StringBuilderDemo_MainForm.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
StringBuilderDemo_MainForm.pas
unit StringBuilderDemo_MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm2 = class(TForm)
    btnSBuilder: TButton;
    gbSize: TGroupBox;
    rbShort: TRadioButton;
    rbLong: TRadioButton;
    ListBox1: TListBox;
    btnString: TButton;
    procedure btnSBuilderClick(Sender: TObject);
    procedure btnStringClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

const
  maxCount = 1000000; // one million
  strSampleShort = 'This is a sample string';
  strSampleLong = 'This is a slightly longer sample string that should be causing some more trouble in .NET when copied over and over so many times in memory, if you use the String class instead of StringBuilder class';

type
  StringBuilder = TStringBuilder;

procedure TForm2.btnSBuilderClick(Sender: TObject);
var
  aTime: TDateTime;
  i, nPos: Integer;
  strB: StringBuilder;
begin
  aTime := Now;

  // actual code
  strB := StringBuilder.Create;
  if rbShort.Checked then
    strB.Append (strSampleShort)
  else
    strB.Append (strSampleLong);
  for I := 1 to maxCount do
  begin
    nPos := I mod strB.Length;
    strB.Remove(nPos, 1);
    strB.Insert(nPos, strB [(I*2) mod strB.Length]);
  end;
  ListBox1.Items.Add (strB.ToString);
  // end of actual code

  // added
  strB.Free;

  aTime := Now - aTime;
  ListBox1.Items.Add('btnSBuilderClick: ' + FormatDateTime ('ss.zzz', aTime));
end;

procedure TForm2.btnStringClick(Sender: TObject);
var
  aTime: TDateTime;
  i, nPos: Integer;
  str: string;
begin
  aTime := Now;

  // actual code
  if rbShort.Checked then
    str := strSampleShort
  else
    str := strSampleLong;

  for I := 1 to maxCount do
  begin
    nPos := I mod Length (str);
    Delete (str, nPos + 1, 1);
    Insert(str [(I*2) mod Length (str) + 1], str, nPos + 1);
  end;
  ListBox1.Items.Add (str);
  // end of actual code

  aTime := Now - aTime;
  ListBox1.Items.Add('btnStringClick: ' + FormatDateTime ('ss.zzz', aTime));
end;

end.
StringBuilderDemo_MainForm.pas.dfm
object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 292
  ClientWidth = 554
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object btnSBuilder: TButton
    Left = 16
    Top = 111
    Width = 105
    Height = 25
    Caption = 'btnSBuilder'
    TabOrder = 0
    OnClick = btnSBuilderClick
  end
  object gbSize: TGroupBox
    Left = 16
    Top = 8
    Width = 105
    Height = 89
    Caption = 'Size'
    TabOrder = 1
    object rbShort: TRadioButton
      Left = 16
      Top = 24
      Width = 57
      Height = 17
      Caption = 'rbShort'
      Checked = True
      TabOrder = 0
      TabStop = True
    end
    object rbLong: TRadioButton
      Left = 16
      Top = 48
      Width = 57
      Height = 17
      Caption = 'rbLong'
      TabOrder = 1
    end
  end
  object ListBox1: TListBox
    Left = 136
    Top = 16
    Width = 410
    Height = 268
    ItemHeight = 13
    TabOrder = 2
  end
  object btnString: TButton
    Left = 16
    Top = 142
    Width = 105
    Height = 25
    Caption = 'btnString'
    TabOrder = 3
    OnClick = btnStringClick
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù