Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Borland Delphi > Lesson 5, Array...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 3702 of 3850
Post > Topic >>

Lesson 5, Arrays, Static, Dynamic, Multi Dimensional (Beyond Excellent)

by "Skybuck Flying" <BloodyShame@[EMAIL PROTECTED] > Apr 19, 2008 at 07:18 AM

Hello,

See comments or website for details

http://members.home.nl/hbthouppermans/D2007Analysis/Index.htm

// *** Begin of AnalysisLesson5.dpr ***

program AnalysisLesson5;

{$APPTYPE CONSOLE}

{

Version 0.01 created on 19 april 2008 by Skybuck Flying

Lesson 5, Arrays, Static, Dynamic, Multi Dimensional.

Conclusion:

Facts:

When it comes to arrays, especially dynamic arrays, Delphi is far superior

than C/C++.

+ Delphi can do range checking for static, dynamic and even dynamic multi 
dimensional arrays.

+ Delphi needs only one line of code to allocate and free arrays.

+ Delphi clears the arrays, which is kinda nice, the programmer doesn't
have 
to write any special clearing code.
- Though this automatic clearing does cost a bit of performance for the 
allocate and free routines.

Extra Delphi Test included to show of Delphi capabilities:

+ Delphi needs only one line of code to re-allocate the arrays without
data 
loss ! very impressive !

Speculation:

+ Also Delphi has a pretty fast memory manager, I wouldn't be surprised if

Delphi's allocation and free routines
  are faster than those of C/C++... However Delphi does clear the arrays
to 
zero, but even with clearing I still
  would not be surprised if Delphi has faster allocation and freeing 
routines...

A c/c++ allocation/free versus Delphi allocation/free performance test
could 
put this speculation to rest...
Though I am not really interesting in it at this moment in time I usually 
just allocate and free arrays once.
However for strings this can be very im****tant, the next lesson will
examine 
strings a little bit better performance
wise... just an instruction count/estimate... nothing fancy.

Score:

20 out of 10

BEYOND EXCELLENT

}

uses
  SysUtils;

type
 Tint8 = shortint;
 Tuint8 = byte;

 Tint16 = smallint;
 Tuint16 = word;

 Tint32 = integer;
 Tuint32 = longword;

 Tint64 = int64;
// Tuint64 = int64; // uint64 is buggy, -1 point for delphi 2007.
 Tuint64 = uint64; // using buggy uint64 implementation for now just to
test 
ranges.

procedure Main;
var
 vStaticArray : array[0..99] of Tint8; // static array
 vDynamicArray : array of Tint8; // dynamic array
 vIndex : Tint8;
 vJndex : Tint8;
 vKndex : Tint8;

 vStaticArray2D : array[0..4, 0..19] of Tint8;
 // alternative way of writing would be:
// vStaticArray2D : array[0..4] of array of [0..19] of Tint8;

 vDynamicArray2D : array of array of Tint8;
 vDynamicArray3D : array of array of array of Tint8;
begin
 writeln('Program Started');
 writeln;

 // one dimensional static array, will be allocated on the stack. (Think
of 
it as tem****arely limited memory)
 for vIndex := 0 to 99 do // 100 would lead to range check error
 begin
  vStaticArray[vIndex] := vIndex;
  writeln('vStaticArray[', vIndex, ']: ', vStaticArray[vIndex] );
 end;


 // one dimensional dynamic array, will be allocated on the heap. (Think
of 
it as main huge memory)
 SetLength( vDynamicArray, 100 );

 for vIndex := 0 to 99 do // 100 would lead to range check error, BETTER 
than c/c++ !
 begin
  vDynamicArray[vIndex] := -vIndex;
  writeln('vDynamicArray[', vIndex ,']: ', vDynamicArray[vIndex] );
 end;

 vDynamicArray := nil;

 // two dimensional arrays, allocation simple, usage simple, BETTER than 
c/c++ !

 // static 2d array
 for vIndex := 0 to 4 do // 5 would lead to range check error, BETTER than

c/c++ !
 begin
  for vJndex := 0 to 19 do // 20 would lead to range check error, BETTER 
than c/c++ !
  begin
   vStaticArray2D[vIndex][vJndex] := (vIndex*20) + vJndex;
   // alternative way of access is:
   // vStaticArray2D[vIndex,vJndex] := (vIndex*20) + vJndex;

   writeln('vStaticArray2D[', vIndex, '][', vJndex, ']: ', 
vStaticArray2D[vIndex][vJndex] );
  end;
 end;

 // dynamic 2d array
 SetLength( vDynamicArray2D, 5, 20 );

 for vIndex := 0 to 4 do // 5 would lead to range check error, BETTER than

c/c++ !
 begin
  for vJndex := 0 to 19 do // 20 would lead to range check error, BETTER 
than c/c++ !
  begin
   vDynamicArray2D[vIndex][vJndex] := (vIndex*20) + vJndex;
   // alternative way of access is:
//   vDynamicArray2D[vIndex,vJndex] := (vIndex*20) + vJndex;

   writeln('vDynamicArray[', vIndex, '][', vJndex, ']: ', 
vDynamicArray2D[vIndex][vJndex] );
  end;
 end;

 vDynamicArray2D := nil;

 // one last example showing a dynamic 3D array, simply extend the
previous 
code with an extra pointer and extra loop for extra dimension:

 // array will be: 2*5*10

 // allocation
 SetLength( vDynamicArray3D, 2, 5, 10 ); // one single line of code sets
all 
3 dimensions, MUCH BETTER than c/c++ !

 // access/usage
 for vIndex := 0 to 1 do // 2 would give a range check error, MUCH BETTER 
than c/c++ !
 begin
  for vJndex := 0 to 4 do // 5 would give a range check error, MUCH BETTER

than c/c++ !
  begin
   for vKndex := 0 to 9 do // 10 would give a range check error, MUCH
BETTER 
than c/c++ !
   begin
    vDynamicArray3D[vIndex][vJndex][vKndex] := (vIndex*5*10) + (vJndex*10)
+ 
vKndex;
    writeln('vDynamicArray3D[', vIndex, '][', vJndex, '][', vKndex, ']: ',

vDynamicArray3D[vIndex][vJndex][vKndex] );
   end;
  end;
 end;

 // extra Delphi-only test, to show off Delphi capabilities:
 // reallocate the array into a bigger array without data loss.
 SetLength( vDynamicArray3D, 4, 7, 12 );

 writeln('re-allocate/scale test, without data loss: ');
 for vIndex := 0 to 1 do // 2 would give a range check error, MUCH BETTER 
than c/c++ !
 begin
  for vJndex := 0 to 4 do // 5 would give a range check error, MUCH BETTER

than c/c++ !
  begin
   for vKndex := 0 to 9 do // 10 would give a range check error, MUCH
BETTER 
than c/c++ !
   begin
    vDynamicArray3D[vIndex][vJndex][vKndex] := (vIndex*5*10) + (vJndex*10)
+ 
vKndex;
    writeln('vDynamicArray3D[', vIndex, '][', vJndex, '][', vKndex, ']: ',

vDynamicArray3D[vIndex][vJndex][vKndex] );
   end;
  end;
 end;

 // show extra data is cleared:
 writeln('show extra data is cleared:');
 for vIndex := 2 to 3 do
 begin
  for vJndex := 5 to 6 do
  begin
   for vKndex := 10 to 11 do
   begin
//    vDynamicArray3D[vIndex][vJndex][vKndex] := (vIndex*5*10) +
(vJndex*10) 
+ vKndex;
    writeln('vDynamicArray3D[', vIndex, '][', vJndex, '][', vKndex, ']: ',

vDynamicArray3D[vIndex][vJndex][vKndex] );
   end;
  end;
 end;

 // free
 // one single line of code cleans it up, MUCH BETTER than c/c++ !
 vDynamicArray3D := nil;

 writeln('Program finished');
end;

begin
 try
  Main;
 except
  on E:Exception do
   Writeln(E.Classname, ': ', E.Message);
 end;
 readln;
end.

// *** End of AnalysisLesson5.dpr ***

This is why I love Delphi :):):)

Bye,
  Skybuck
 




 1 Posts in Topic:
Lesson 5, Arrays, Static, Dynamic, Multi Dimensional (Beyond Exc
"Skybuck Flying"  2008-04-19 07:18:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Jul 23 21:27:17 CDT 2008.