Recent

Author Topic: Initializing an array  (Read 764 times)

stoffman

  • Jr. Member
  • **
  • Posts: 67
Initializing an array
« on: May 31, 2023, 08:29:15 am »
I need to Initialize an array of unknown size with the index as the value of each element. The obvious solution is to use a loop:

Code: Pascal  [Select][+][-]
  1. for i:=0 to length(arr)-1 do
  2.    arr[i]:=i;


I wonder if there is a faster option?

Thanks

ojz0r

  • Jr. Member
  • **
  • Posts: 58
Re: Initializing an array
« Reply #1 on: May 31, 2023, 08:56:08 am »
Not an answer to your question but use "for i := low(arr) to high(arr) do", then you wont run into trouble if the array doesnt start at 0.

Edit: On the other hand, a dynamic array always starts at 0 so in that case its known...Yet i think the syntax looks clearer :)
« Last Edit: May 31, 2023, 08:59:18 am by ojz0r »
Just trying to learn.

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: Initializing an array
« Reply #2 on: May 31, 2023, 12:45:50 pm »
AVX would probably be the fastest way

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Initializing an array
« Reply #3 on: May 31, 2023, 02:08:54 pm »
you need an intrinsic binary increment fill function which I don't see in the list of functions in the system unit?

if you are doing this many times, then I would suggest you could map a reference array to its largest ever size and then use the MOVE function each time, whereas this operation uses a more efficient way to transfer memory.

 Also, I don't know how to write such a code with a inline assembler but it wouldn't be cross platform.

 using a String copy ASM feature to move the counter in as it goes alone.

 I guess it depends on how serious you are on speed.
The only true wisdom is knowing you know nothing

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Initializing an array
« Reply #4 on: May 31, 2023, 02:44:54 pm »
I need to Initialize an array of unknown size with the index as the value of each element. The obvious solution is to use a loop:

Code: Pascal  [Select][+][-]
  1. for i:=0 to length(arr)-1 do
  2.    arr[i]:=i;


I wonder if there is a faster option?

Thanks

The fastest way would be to not do it at all.

To be less obtuse - if you have a fixed line of modifications you apply to the array afterwards then simply integrate the setting into the first of these calculations. So, instead of

Code: Pascal  [Select][+][-]
  1. for i:=Low(arr) to High(arr) do
  2.    arr[i]:=i;
  3. ModifyStep1(arr);
  4. ModifyStep2(arr);
  5.  

you change 'ModifyStep1' in a way that it integrates arr:=i. You can play around with this approach - maybe you need two variants of ModifyStep1, one integrating the setting, one without, etc. Can't comment more specific here, as I don't know your specific requirements.

Cheers,
MathMan

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: Initializing an array
« Reply #5 on: May 31, 2023, 05:35:14 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. Label One, Two;
  3. Var
  4.   A:Array of Integer;
  5.   P:PInteger;
  6.   C,M:Integer;
  7. begin
  8.   SetLength(A, 1000);
  9.   // Start of  incremental fill code.
  10.   M:= Length(A);
  11.   C := 0;
  12.   P := @A[0];
  13. One:      //loop here.
  14.   Inc(C);
  15.   IF C >= M Then
  16.     Goto two;
  17.   P^:=C;
  18.   Inc(P);
  19.   Goto One;
  20. Two:
  21. end;                
  22.  

If you look at the ASM code on that loop, its about as close as it gets without going into ASM.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018