Initializing an array lazarus / 21-General ---------------------------------------------------------------------- stoffman · 2023-05-31 06:29 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i:=0 to length(arr)-1 do   arr[i]:=i; I wonder if there is a faster option? Thanks ---------------------------------------------------------------------- ojz0r · 2023-05-31 06:56 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 ---------------------------------------------------------------------- BeniBela · 2023-05-31 10:45 AVX would probably be the fastest way ---------------------------------------------------------------------- jamie · 2023-05-31 12:08 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. ---------------------------------------------------------------------- MathMan · 2023-05-31 12:44 Quote from: stoffman on May 31, 2023, 08:29:15 amI 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i:=0 to length(arr)-1 do   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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i:=Low(arr) to High(arr) do   arr[i]:=i;ModifyStep1(arr);ModifyStep2(arr);  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 · 2023-05-31 15:35 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);Label One, Two;Var  A:Array of Integer;  P:PInteger;  C,M:Integer;begin  SetLength(A, 1000);  // Start of  incremental fill code.  M:= Length(A);  C := 0;  P := @A[0];One:      //loop here.  Inc(C);  IF C >= M Then    Goto two;  P^:=C;  Inc(P);  Goto One;Two:end;                 If you look at the ASM code on that loop, its about as close as it gets without going into ASM.