Arrays

Started by Pfaber11, October 06, 2021, 09:47:48

Previous topic - Next topic

Pfaber11

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.
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz

Derron

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

TomToad

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.

Derron

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

Steve Elliott

Yes AGK arrays are fast and work as a linked list where appropriate.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

blinkok

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

MikeHart

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.

Derron

Quote from: MikeHart on October 07, 2021, 07:21:30
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 from: blinkok on October 06, 2021, 20:55:16
QuoteIf 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

MikeHart

Yes Deron, that repository.

Pfaber11

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. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz