October 27, 2021, 21:46:41

Author Topic: Arrays  (Read 680 times)

Offline Pfaber11

  • Hero Member
  • *****
  • Posts: 823
  • Bonjour
Arrays
« on: October 06, 2021, 09:47:48 »
I have been at this a few years now and have just researched arrays . Never needed to create one so far in all this time . Anyway when I do need to use one I'll have a basic knowledge of how they work and am confident that I can do this.
« Last Edit: October 06, 2021, 10:18:49 by Pfaber11 »
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3675
Re: Arrays
« Reply #1 on: October 06, 2021, 10:47:02 »
Arrays are good if you have a predefined amount of elements (or a kind of "maximum") as it does not need much "management" when adding or sorting elements. If you do not know what amount you have, you can use a linked list or map or hash map (or whatever your language kit offers). They then have the container (list) and elements either directly inheriting from "children" or there is a children which has the needed functionality and holds itself the object you add.
this means you often have an "overhead" which an array does not have. Also direct access to elements is fast (via a simple numeric index). increasing an array or decreasing it means to copy the whole memory block of an array (means 100 elements = 100 "integers", the objects itself are not copied - of course). Sounds not much, but if you have an array of 10.000 elements, this means 10.000*4byte = 40kbyte of memory "recreated".
This makes it obvious that you better do NOT resize an array each frame (or worst case even more of these arrays per frame).


bye
Ron

Offline TomToad

  • Hero Member
  • *****
  • Posts: 557
Re: Arrays
« Reply #2 on: October 06, 2021, 17:42:34 »
I believe that AGK arrays are implemented internally as an array list.  Adding new elements and resizing arrays are very quick.  There is even a method myArray.insertSorted() which inserts elements in proper order, so you don't need to call sort afterwards.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3675
Re: Arrays
« Reply #3 on: October 06, 2021, 18:14:22 »
If you add something to an array and want it to be "sorted" it of course is faster than ordering the complete array.
All other elements stay unchanged - so isnt it just a matter of finding the first one being "bigger" and then you have your order ready: "smaller - new item - bigger".

Maybe someone knows how arrays are implemented internally in AGK - and if they are faster than their alternatives (if there are any - am not using AGK nor ever used it)


bye
Ron

Offline Steve Elliott

  • Hero Member
  • *****
  • Posts: 3237
  • elgol
Re: Arrays
« Reply #4 on: October 06, 2021, 20:35:24 »
Yes AGK arrays are fast and work as a linked list where appropriate.
Windows 10 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
MacOS Big Sur 64-bit, 8Gb RAM, Intel i5 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB
Linux Mint 19.3 64-bit, 16Gb RAM, Intel i5 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb)
Raspberry pi 3, pi 4, pi 400, BBC B, C64, ZX Spectrum

Offline blinkok

  • Sr. Member
  • ****
  • Posts: 381
Re: Arrays
« Reply #5 on: October 06, 2021, 20:55:16 »
Quote
If you add something to an array and want it to be "sorted" it of course is faster than ordering the complete array
It is my experience that this is very very slow. I would recommend, if possible,  using array.sort() once the array is created.

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 818
Re: Arrays
« Reply #6 on: October 07, 2021, 07:21:30 »
The whole source code to Classic is on GH so it can be investigated.

One thing that newcomers to Arrays in AGK stumble upon is Array.Length which returns actually the index of the last element.

Offline Derron

  • Hero Member
  • *****
  • Posts: 3675
Re: Arrays
« Reply #7 on: October 07, 2021, 08:10:48 »
The whole source code to Classic is on GH so it can be investigated.

Only found
https://github.com/TheGameCreators/AGKTier2

Are you talking about this?


Array.length = index indicates that it is more of an "managed array" or so (similar to Bruceys brl.mod/ObjectList.mod).


Quote
If you add something to an array and want it to be "sorted" it of course is faster than ordering the complete array
It is my experience that this is very very slow. I would recommend, if possible,  using array.sort() once the array is created.
Then this is because of the implementation in AGK. For simple arrays it should be faster to only find out the "new index" of a new item (if all others are sorted already) than having to compare all elements versus the others to find out their positions (some keep their position, all others will be offset by 1).

bye
Ron

Offline MikeHart

  • Hero Member
  • *****
  • Posts: 818
Re: Arrays
« Reply #8 on: October 07, 2021, 13:50:57 »
Yes Deron, that repository.

Offline Pfaber11

  • Hero Member
  • *****
  • Posts: 823
  • Bonjour
Re: Arrays
« Reply #9 on: October 07, 2021, 20:05:08 »
I might just play around and see what I can come up with. It's only recently I decided to find out about them . I remember back in the zx81 days people talking about arrays and I just didn't get it , really didn't have the foggiest idea what they were on about . just lately I've watched some vids on you tube and the guy explains what they are and how to use them , in English. They are no longer a mystical thing and I hope I get the chance to use one in my next project . The way I think about them is they are like a special variable but instead of storing one piece of information they store many pieces of information. I think that is what they do anyway. I will go out of my way to include one in my next project .
Tree x,y coordinates for my terrain that sort of thing .
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen.

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal