1. Arrays are slower than normal variables
The following code is inefficient:
Код:
new Float:pos[3];
GetPlayerPos(playerid, pos[0], pos[1], pos[2]);
And here is the assembly version of that above code:
Код:
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
Now here is an equivalent code written more efficiently:
Код:
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y , z);
And here is the assembly version:
Код:
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
When you want to access an array element, the compiler uses the following algorithm:
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