Variable
#1

How is better and fast?

using this:

Код:
new myvar;
new myvar2;
new myvar3;
this:

Код:
new myvar[3];
or this?

Код:
new myvar, myvar2, myvar3;
Or is the same?

and another doubt...



enumerators increase the .amx?
Reply
#2

Quote:
Originally Posted by Yashas
Посмотреть сообщение
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
i hope this answer your question 1st question,

about enum, i dont think it increases ur amx size
Reply
#3

Quote:
Originally Posted by ReD_HunTeR
Посмотреть сообщение
about enum, i dont think it increases ur amx size
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
Reply
#4

Quote:
Originally Posted by kvann
Посмотреть сообщение
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
This is also explained somewhere in pawn-lang.dff, enums just exist for the compiler, basically all enum entries get replaced by indexes when compiling and the arrays are just regular arrays.
So having this enum:

Код:
enum E_TEST
{
e_val,
e_array[10]
};
new array[E_TEST];
will result in a 1D (not 2D) array with the size of 11 cells. e_val will be at index 0 and e_array at index 1 - 10.

So writing a string to e_array using strcat would result in something like this:

Код:
strcat(array[1], "mytext", 10);
This will write to the array beginning at index 1 with a size of 10 (11-1).
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)