Languages & Coding > AppGameKit ( AGK )

Arrays

(1/2) > >>

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.

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.

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.

Navigation

[0] Message Index

[#] Next page

Go to full version