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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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
Systems 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
Author 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
Systems 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
Commented:
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
Commented:
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
Author 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:

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

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

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
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

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
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
Systems 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
Commented:
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
Systems 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
Commented:

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
Author 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Pascal

From novice to tech pro — start learning today.