Algorithm Math Delphi

Title: Dynamic array.
Question: How to create a dynamic array in Delphi.
Answer:
Once more about dynamic array in Delphi. Delphi allows more than one method of dynamic array creating, and the use of TList object is less efficient.
Code, presented below, shows 4 different methods to allocate dynamic array.
GlobalAlloc function appeared to be the fastest one.
unit UTest2;
{project 02-01-2000 }
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Label1: TLabel;
Button1: TButton;
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
ListArray : TList;
fSize : integer;
dnArray : array of double;
testCount : word;
procedure TestdnArray;
procedure TestpDelphiArray;
procedure TestpWin32Array;
procedure TestTList;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
type
TDinArray = array[0..0] of double;
PDinArray = ^TDinArray;
const
arlength = 1000000; //size of array
var
pWin32Array : PDinArray;
pDelphiArray : PDinArray;
procedure TForm1.TestdnArray; // Dynamic array (Delphi 4)
var startTime,delta, i : longint;
sum : double;
cm, m : string;
begin
sum := 0;
cm := 'Dynamic array (Delphi 4). Consumed ';
startTime := GetTickCount();
SetLength(dnArray, arLength);
// assign values to the array members
for i := 0 to arLength - 1 do
dnArray[i] := i;
// find the sum of all members
for i := 0 to arLength - 1 do
sum := sum + dnArray[i];
// get time
delta := GetTickCount() - startTime;
FmtStr(m,'%s %d ms',[cm,delta]);
ListBox1.Items.Add(m);
// clean
SetLength(dnArray, 0);
end;
procedure TForm1.TestpDelphiArray;
// Creates pointer to the array of double using AllocMem( fSize );
var startTime,delta, i : longint;
sum : double;
cm, m : string;
begin
sum := 0;
cm := 'Allocate Memory function. Consumed ';
startTime := GetTickCount();
// create
pDelphiArray := AllocMem( fSize );
// assign values to the array members
for i := 0 to arLength - 1 do
pDelphiArray^[i] := i;
// find the sum of all members
for i := 0 to arLength - 1 do
sum := sum + pDelphiArray^[i];
// get time
delta := GetTickCount() - startTime;
FmtStr(m,'%s %d ms',[cm,delta]);
ListBox1.Items.Add(m);
// clean
FreeMem( pDelphiArray, fSize);
end;
procedure TForm1.TestpWin32Array;
// Creates pointer to the array of double using GlobalAlloc function;
var startTime,delta, i : longint;
sum : double;
cm, m : string;
HNDL : integer;
begin
sum := 0;
cm := 'GlobalAlloc function. Consumed ';
startTime := GetTickCount();
// create
HNDL := GlobalAlloc(GHND,fSize);
pWin32Array := GlobalLock(HNDL);
// assign values to the array members
for i := 0 to arLength - 1 do
pWin32Array^[i] := i;
// find the sum of all members
for i := 0 to arLength - 1 do
sum := sum + pWin32Array^[i];
// get time
delta := GetTickCount() - startTime;
FmtStr(m,'%s %d ms',[cm,delta]);
ListBox1.Items.Add(m);
// clean
if HNDL 0 then GlobalFree(HNDL);
end;
procedure TForm1.TestTList;
// Creates pointer to the array of double using GlobalAlloc function;
var startTime,delta, i : longint;
sum : double;
cm, m : string;
dbl : ^double;
begin
sum := 0;
cm := 'TList function. Consumed ';
startTime := GetTickCount();
// create
ListArray := TList.Create;
// assign values to the array members
for i := 0 to arLength - 1 do begin
new(dbl); dbl^ := i;
ListArray.Add(dbl);
end;
// find the sum of all members
for i := 0 to arLength - 1 do begin
dbl := ListArray[i];
sum := sum + dbl^;
end;
// get time
delta := GetTickCount() - startTime;
FmtStr(m,'%s %d ms',[cm,delta]);
ListBox1.Items.Add(m);
// clean
ListArray.Clear;
ListArray.Free;
end;
procedure TForm1.FormActivate(Sender: TObject);
var m : string;
i : cardinal;
begin
TestCount := 1;
fSize := arlength * SizeOf(Double);
m := 'Project tests the spead of creation and computing with ' + #13;
m := m + 'pointer of Array, dynamic array (Delphi 4), and TList.'+ #13;
m := m + 'Number of items in each array is : '+ IntTostr(arlength) + #13;
m := m + 'Minimum memory needed : ' + IntTostr(fSize) + ' bytes' + #13;
m := m + 'Repeat test 2-3 times for more precise data.';
Label1.Caption := m;
end;
//
procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Items.Add(IntToStr(TestCount));
TestdnArray;
TestpDelphiArray;
TestpWin32Array;
TestTList;
Inc(TestCount);
end;
end.