Recent

Author Topic: [RESOLVED] Add integers to an array  (Read 1102 times)

Pixy

  • New Member
  • *
  • Posts: 40
Re: [RESOLVED] Add integers to an array
« Reply #15 on: May 21, 2019, 02:11:40 am »
There shouldn't be (much) trouble if the arrays are reasonably short, say up to about a hundred integers. It depends also on how many times is "multiple times".

There are solutions even for high update rates and huge arrays but without knowing more of what you are doing they may be a little overkill for you.

I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
« Last Edit: May 21, 2019, 02:15:49 am by Pixy »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 822
Re: [RESOLVED] Add integers to an array
« Reply #16 on: May 21, 2019, 03:08:38 am »
There shouldn't be (much) trouble if the arrays are reasonably short, say up to about a hundred integers. It depends also on how many times is "multiple times".
There are solutions even for high update rates and huge arrays but without knowing more of what you are doing they may be a little overkill for you.
I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
Less than 1 second here for 300,000 ten-digit elements.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   TimeLapsed: TTime;
  4.   Number: Integer;
  5.   i, Elements, ArraySize: Integer;
  6.   IntAr: array of Integer;
  7. begin
  8.   TimeLapsed := Time;
  9.   Elements   := 300000;
  10.   Number     := 1234567890;
  11.  
  12.   for i := 1 to Elements do
  13.   begin
  14.     ArraySize := i;
  15.     setLength(IntAr, ArraySize);
  16.     IntAr[ArraySize - 1] := Number + i;
  17.   end;
  18.  
  19.   TimeLapsed := Time - TimeLapsed;
  20.   ShowMessage('Time-lapsed: ' + TimeToStr(TimeLapsed) + LineEnding +
  21.               'IntAr[' + IntToStr(Elements - 1) + '] = ' + IntToStr(IntAr[Elements - 1]) + LineEnding +
  22.               'Array Size: ' + IntToStr(Length(IntAr)));
  23. end;[code]

lucamar

  • Hero Member
  • *****
  • Posts: 2018
Re: [RESOLVED] Add integers to an array
« Reply #17 on: May 21, 2019, 04:02:12 am »
I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.

Test it and see how much time it takes using SetLength(Length+1) each time you want to add one item in your target platform. A maximum of 210 item/second doesn't sound as much for current OS/hardware.

Of course, depends on what else the application must do :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 3149
Re: [RESOLVED] Add integers to an array
« Reply #18 on: May 21, 2019, 09:57:05 am »
If an upper limit is known in advance, simply SetLength(arry, 210); at the outset.
Keep a count of the number of indexes allocated, and when all indexing is complete, call SetLength(arry, count).
Of course, the difference in speed compared to multiple calls to SetLength() may be negligible. Timing the two alternative implementations is the only way to know if there is any significant speed gain.  It is surprising how often  optimisations which in theory would be expected to make a difference in practice make little difference, or even have a negative effect with certain data. Compilers are getting ever more sophisticated, and low level caching/pipelining etc. over which the high level programmer has little or no control may ultimately prove to be the determinative factor.

Pixy

  • New Member
  • *
  • Posts: 40
Re: [RESOLVED] Add integers to an array
« Reply #19 on: May 21, 2019, 02:42:54 pm »
I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.

Test it and see how much time it takes using SetLength(Length+1) each time you want to add one item in your target platform. A maximum of 210 item/second doesn't sound as much for current OS/hardware.

Of course, depends on what else the application must do :)

It's basically a script for a bot and the array adds coordinates as needed. Despite that I am also looking at the bigger picture by learning and testing out Pascal for the purpose of understanding its capacity, applicability and potential use. I could already make what I wanted to do to work, I am just trying to figure out betterment and optimization to do the same thing by combining your ideas guys. Thanks for your input.

There shouldn't be (much) trouble if the arrays are reasonably short, say up to about a hundred integers. It depends also on how many times is "multiple times".
There are solutions even for high update rates and huge arrays but without knowing more of what you are doing they may be a little overkill for you.
I think that more than 210 indexes wont be needed. So, anywhere between 3 to 210 (each index with integer of up to 6-7 digits) within let's say a second or as fast as it can be done.
Less than 1 second here for 300,000 ten-digit elements.

Code: Pascal  [Select]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   TimeLapsed: TTime;
  4.   Number: Integer;
  5.   i, Elements, ArraySize: Integer;
  6.   IntAr: array of Integer;
  7. begin
  8.   TimeLapsed := Time;
  9.   Elements   := 300000;
  10.   Number     := 1234567890;
  11.  
  12.   for i := 1 to Elements do
  13.   begin
  14.     ArraySize := i;
  15.     setLength(IntAr, ArraySize);
  16.     IntAr[ArraySize - 1] := Number + i;
  17.   end;
  18.  
  19.   TimeLapsed := Time - TimeLapsed;
  20.   ShowMessage('Time-lapsed: ' + TimeToStr(TimeLapsed) + LineEnding +
  21.               'IntAr[' + IntToStr(Elements - 1) + '] = ' + IntToStr(IntAr[Elements - 1]) + LineEnding +
  22.               'Array Size: ' + IntToStr(Length(IntAr)));
  23. end;[code]

Wow. I'll definitely test that.

If an upper limit is known in advance, simply SetLength(arry, 210); at the outset.
Keep a count of the number of indexes allocated, and when all indexing is complete, call SetLength(arry, count).
Of course, the difference in speed compared to multiple calls to SetLength() may be negligible. Timing the two alternative implementations is the only way to know if there is any significant speed gain.  It is surprising how often  optimisations which in theory would be expected to make a difference in practice make little difference, or even have a negative effect with certain data. Compilers are getting ever more sophisticated, and low level caching/pipelining etc. over which the high level programmer has little or no control may ultimately prove to be the determinative factor.

Are you suggesting to use setlength one time with the upper limit (210) and then as integers are added to the array's indexes within the loop, finally return the number of indexes that have been occupied and then reduce the length from 210 to the number of indexes occupied by integers? If so it is not a bad idea. It went by my head, but I didn't give this idea a shot.

Kays

  • Full Member
  • ***
  • Posts: 182
  • Whasup!?
    • KaiBurghardt.de
Re: Add integers to an array
« Reply #20 on: May 21, 2019, 03:07:35 pm »
[…] but I get an access violation error. […]
[…] An "array of integer" is just a pointer to an array. […]
Just to explain the error message: Your IntAr (as a “managed” type) is initialized as a nil pointer, i. e. it points to the address 0. The “access violation” occurs, because your program accesses a block that isn’t in its purview. If you coincidentally happen to access IntAr[1234567] and that block belongs to your program, you could alter its content without an error.

Only a {$rangeCheck on} will trigger appropriate run-time errors. {$checkPointer on} will still grant access via a nil-pointer if the indices are within the program’s bounds.
« Last Edit: May 26, 2019, 05:27:40 pm by Kays »
Yours Sincerely
Kai Burghardt