Strings Delphi

Title: Speeding up your application by passing Strings as Const
Question: This example demonstrates the increase in speed when you pass a string to a function / procedure as Const. This is useful when you do not need to modify the contents of the string within the procedure or function.
Results : passing 20 char string 10,000,000 times
Passing strings normally.................................711 msecs
Passing strings as Const..................................160 msecs
My Delphi stuff at Lummie.co.uk
Answer:
Written by Simon Moscrop
// Start of DFM
object Form1: TForm1
Left = 200
Top = 112
Width = 311
Height = 152
Caption = 'Testing parameter passing speed'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 8
Top = 56
Width = 141
Height = 13
Caption = 'Speed for pass by constant ...'
end
object Label2: TLabel
Left = 8
Top = 88
Width = 126
Height = 13
Caption = 'Speed for pass by value ...'
end
object Button1: TButton
Left = 8
Top = 8
Width = 289
Height = 41
Caption = 'Start Test'
TabOrder = 0
OnClick = Button1Click
end
end
// End of DFM
unit frmStringTest;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure DoStringByValue(strString: String);
procedure DoStringAsConst(const strString: String);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var
Start, Finish, Start1, Finish1: TTimeStamp;
iCounter1, iCounter2: Integer;
begin
Cursor := crHourglass;
Button1.Enabled := False;
// Pass by constant test
Start := DateTimeToTimeStamp(Now);
for iCounter1 := 0 to 10000000 do
begin
DoStringAsConst('ABCDEFGHIJKLMNOPQRST');
end;
Finish := DateTimeToTimeStamp(Now);
Label1.Caption := 'Passsing string by constant takes ' + (IntToStr(Finish.Time - Start.Time)) + ' milliseconds';
Start1 := DateTimeToTimeStamp(Now);
for iCounter2 := 0 to 10000000 do
begin
DoStringByValue('ABCDEFGHIJKLMNOPQRST');
end;
Finish1 := DateTimeToTimeStamp(Now);
Label2.Caption := 'Passsing string by value takes ' + (IntToStr(Finish1.Time - Start1.Time)) + ' milliseconds';
Cursor := crDefault;
Button1.Enabled := True;
end;
procedure TForm1.DoStringByValue(strString: String);
var
x: Integer;
begin
X := X + 1;
end;
procedure TForm1.DoStringAsConst(const strString: String);
var
X: Integer;
begin
X := X + 1;
end;
end.