new myvar; new myvar2; new myvar3;
new myvar[3];
new myvar, myvar2, myvar3;
1. Arrays are slower than normal variables
The following code is inefficient: Код:
new Float:pos[3]; GetPlayerPos(playerid, pos[0], pos[1], pos[2]); Код:
zero.pri addr.alt fffffff4 fill c ;These 3 instructions are responsible for zeroing all the array elements break ; 38 addr.pri fffffff4 ;Get the address of the array add.c 8 ;Add the index (index 2 means 2*4 bytes ahead) load.i ;This will get the value stored at that address push.pri ;Now push the argument addr.pri fffffff4 ;Same as above add.c 4 load.i push.pri addr.pri fffffff4 ;Same as above load.i push.pri Код:
new Float:x, Float:y, Float:z; GetPlayerPos(playerid, x, y , z); Код:
push.c 0 //Making room for the variables on the stack push.c 0 push.c 0 push.adr fffffff4 //Pushing the arguments push.adr fffffff8 push.adr fffffffc Address of first element + 4*Index = the location where Array[Index] is stored (This formula is true only for 1-dimensional arrays) After computing the address of the array element, the data stored in the element can be retrieved. This doesn't mean that you must not use arrays. You must instead use arrays wisely. Do not make arrays for no reason when the same thing can be simply done using normal variables. In my opinion using x, y, z is actually more readable than using an array pos[3]. Speed Tests: Array (10 Assignments):2444,2448,2473 Non-Array (10 Assignments):972,975,963 Speed Test Code:http://pastebin.com/aMkNtaC2 |
I did some not very thorough tests and from what I can see, enumerated arrays generate the same assembly (excluding the line numbers in comments) as their non-enumerated counterparts and there is no trace of the enum itself either. Here's a comparison of my test codes and their generated assemblies: https://www.diffchecker.com/E0TLWHpm
|
enum E_TEST { e_val, e_array[10] }; new array[E_TEST];
strcat(array[1], "mytext", 10);