reading Array with pointers

Is it possible to use pointers to read/search fore a value in an either a row or a collom. I.e. by incrementing a pointer by a value?
If it is possible is it then faster than than making a normal for to loop to search the array?
KLAUSAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
HypoConnect With a Mentor Commented:
I guess that every time you need to access a 2D array, the compiler needs to calculate wich position in the array
[3,k] or [k,3] represents... dooing this means that it has to use a simple formula...

DataBlock : array [0..99,0..49] of longint;

For the array "DataBlock"
SizeOfFirstDimension = 100
SizeOfSecondDimension = 50
SizeOfArrayType = 4 (longint = 4 bytes)

As far as I know this calculation for DataBlock[3,k] would be: (3 * SizeOfSecondDimension + k) * SizeOfArrayType.
and for DataBlock[k,3] this would be (k * SizeOfSecondDimension + 3) * SizeOfArrayType.

Performing this calculation takes a couple of cycles each time it needs to be performed... e.g. each time you use the array.

So, It would be faster to precalculate an incremental value for the array wich you add to the pointer whenever you want to get the next element in the array.

DataBlock : array[0..Y,0..X] of longint;

Retreaving the Incremental value for the Y dimension in this case above would be IncValue := (X+1)*SizeOfArrayType;
(since longint = 4 this example could be optimized to IncValue := (X+1) shl 2;)
Gettin' the IncrementalValue for the  X dimension in the array abocve would simply be IncValue := SizeOfArrayType;

I don't think that it will make that much of a difference in speed... But if you want to know that you've done everything you can to make it as fast as possible... My advise to you is to use and increase the pointer. I think that's the fastest way.
0
 
dbruntonCommented:
I think you need to post some code here first.  What type of array are we talking about?

Are we taliking about arrays of number or strings?  And what dimensions?

Could you be talking about an array of pointers?

Do you just want to see if the value is in the array and not care about about returning its position?

In either case by incrementing the pointer - which I don't think is possible in Pascal but you can do in C - or incrementing your row/column count would take roughtly the same time.

I doubt if you would make much savings in time becuase you still must do a limit check each time to see if you are still withing the arrray with either of the two methods.
0
 
My name is MudSystems EngineerCommented:
The For or Loops won't have much impact on the timming, but when trying to move an array, it is better to use pointers with MOVE...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
KLAUSAuthor Commented:
We are talking about a 2 dimensional array of integers/reals.

I would like to search either a row or a col for either max or min and retrive the value.

I am using GNU Pascal which allows some pointer operations like inc dec + - ect.
0
 
My name is MudSystems EngineerCommented:
MyPointer:=@MyArrayInt;

For I:=0 To NMax Do
  For J:=0 To MMax Do
    If Integer(Ptr(Seg(MyPointer^),Ofs(MyPointer^)+I*J*SizeOf(Integer))) = SeekValue Then
      Begin
        Found:=True;
        Break
      End;
0
 
Sasha_MCommented:
It's definitely possible, but I'm not sure how much faster this will be than a for loop, accessing the array by index.
0
 
Sasha_MCommented:
It's definitely possible, but I'm not sure how much faster this will be than a for loop, accessing the array by index.
0
 
KLAUSAuthor Commented:
What I had in mind was some to replace a search like this:

min := max_integer;

for k := 1 to 100 do
 begin
  if array[3,k]< min then
    min := array[3,k];
 end;
...

or

for k := 1 to 100 do
begin
 if array[k,3] < min then
   min := array[k,3];
end;

solved by soemthing like:

start_addr := @Array[3,1];
end_addr := @array[3,100];

(?? incrementing value for row ??)
(?? and for col??)

ptr := start_addr;

repeat
if ptr^.value < min then
  min:=ptr^.value;
ptr := ptr + (?? incrementing value??);
until ptr > end_addr;

regarding speed is it faster to access by pointer and do one add operation and two comparision ?

The typical array that I need to acces is may 5k-20k items symetric. But I have to do it alot of time.

yes I know that if I'am speed crazy the pascal is not the language, but it is the only one that I can program.
 

while
0
 
VIBESPROCommented:
Actually there is no BIG difference in speed!  If you use a pointer with the move procedure if would be faster that way.  Instead of pointing to each position in the array.

Hypo makes some sense there try it out!
0
 
My name is MudSystems EngineerCommented:
One important question??? are you trying to run the code on a 486??? if that's not the case, the pentium and up, don't give a shitte about timming, so even if your code ain't the fastest, it should fly on a pentium, so why bother with some fancy code???


Now give me the points...
0
 
dbruntonCommented:
OK so you can increment and decremt pointers.

From your other comments I would use the array method rather than the pointer method and get the program running first.  If then you decide that you need more speed investigate the pointer method.

Or try both methods and time then and choose.  I think there's enough code examples for the pointer method there.


Persoanlly I would use the array method for the better chance of catching run time errors than with the pointers and to H*** with the speed.
0
 
My name is MudSystems EngineerCommented:
MyPointer:=@MyArrayInt;

TotalItems:=NMax*MMax;
ActualItem:=0;
While (Not(Found)) And (ActualItem <= TotalItems) Do
  Begin
    If Integer(Ptr(Seg(MyPointer^),Ofs(MyPointer^)+ActaulItem*SizeOf(Integer)))= SeekValue Then
      Begin
          Found:=True;
      End;
    ActualItem:=ActualItem+2
  End;
0
 
VIBESPROCommented:
KLAUS! regarding your question..

to the way you want to use the pointer and array, there will be NO difference in speed between the two.   Both of them are managed between the compiler and the processor.  the only thing about the two is that arrays return errors while pointers crash your system
if there a value out of their boundaries.

the array is better to use for simple tasks like what you have in mind
and
the pointer is better for blockreading
0
 
KLAUSAuthor Commented:
Sorry that I have not closed the question before, but I did not have acces to the internet before.

This answar clear how I calculate the incrementing value both for x and y and is given in a clear example.

Thanks for all the other answars
0
All Courses

From novice to tech pro — start learning today.