open.mp forum
[Pawn] Optimization in pawno. - Printable Version

+ open.mp forum (https://forum.open.mp)
-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
--- Thread: [Pawn] Optimization in pawno. (/showthread.php?tid=1405)



Optimization in pawno. - AsteriskRin - 2020-12-03

Hello. I am still new at pawno. Could you tell me which one is more efficient from these code? These code has the same purpose.



First Code

Code:
forward test();

public test();

{

? ? new rows;

? ? for(new i = 0; i < rows; i) {

? ? ? ? new string[128];

? ? ? ? format(string, sizeof string, "%d is printed.", i);

? ? ? ? SendClientMessage(0, 0xFFFFFFFF, string);

? ? }

}



Second Code

Code:
forward test();

public test();

{

? ? new rows;

? ? new string[128];

? ? for(new i = 0; i < rows; i) {

? ? ? ? format(string, sizeof string, "%d is printed.", i);

? ? ? ? SendClientMessage(0, 0xFFFFFFFF, string);

? ? }

}



RE: Optimization in pawno. - Awide - 2020-12-03

I am no backend expert but the second method seems to be more efficient, since that doesn't create a new valuable every single time.

Although this code wouldn't display anything, since rows are equal to 0.


RE: Optimization in pawno. - Pinch - 2020-12-03

Code:
forward test();

public test();

{

    new

        rows,

        string[128];



    for(new i = 0; i < rows; i) {

        format(string, sizeof string, "%d is printed.", i);

        SendClientMessage(0, 0xFFFFFFFF, string);

    }

}



RE: Optimization in pawno. - Kwarde - 2020-12-04

Let's ignore the misplaced semicolon after public test() in both cases.

You don't have to worry about optimizations like these runtime wise. From https://github.com/pawn-lang/YSI-Includes/blob/5.x/YSI_Core/y_profiling/features.md

Quote:Modern computers are also very fast. They literally do milliards of things a second. Timing something that takes only a few thousand instructions is such a small time as to be almost unnoticable - the time can get lost in noise.



As for efficiency, if you're not the variable elsewhere in the function (like this test() function) it doesn't really matter. If the array is manipulated somewhere in that code (like in the example below [which doesn't really make sense, as in it's a useless piece of code]) you'll be left with weird values for that array:



Code:
someFunction()

{

    new pName[MAX_PLAYER_NAME];

    for (new i, j = GetPlayerPoolSize(); i <= j; i)

    {

        GetPlayerName(playerid, pName, MAX_PLAYER_NAME);

        printf("playerid %d is called %s", playerid, pName);

    }

    pName[0] = EOS; //EOS => EndOfString (null terminator, \0). PAWN uses EOS in all string functions: strlen() counts the amount of characters untill it reaches EOS, for example.

    //I've seen scripts using this to "empty a string". It doesn't, it just appears empty for PAWN string functions because the first character is EOS.

    //Example below shows that

    for (new i; i < 4; i)

    {

        switch (i)

        {

            case 0: pName[0] = 'r';

            case 1,2: pName[i] = 'o';

            case 3: pName[3] = 't';

        }

    }

    print(pName);

}



Let's say just one player (you, on localhost, with name AsteriskRin) is online when this code runs. Server log would output this:

Code:
AsteriskRin

rootriskRin

In this case you would have to truly empty the entire array, or simply use a different one.



If you have some time, you might enjoy reading this document: https://github.com/YashasSamaga/AMX-Assembly-Docs/blob/master/DOCUMENT.md - it will give you a better understanding of how things work like how and where local (and global/static local) variables are declared.



To sum it up once again: second code would be the best "optimized" but especially in this Test() example it does not matter at all.


RE: Optimization in pawno. - gzxmx94 - 2020-12-07

CMIIW: The only thing a `new` usually does is, initialize a variable. You don't allocate any variables at runtime, because PAWN is a statically compiled language - there are no runtime allocations*.

While the second example might theoretically be faster.. instead of thinking about micro- and premature optimization, think about code readability (magic numbers, variable names, function names, layout of the code, indentation, verbosity), design (apply programming patterns that suit the use case, manage responsibilities, don't make spaghetti-intertwined dependencies, dependency loops, logical units/modules, etc), profiling real bottlenecks and future maintainability (would you, or someone else, still understand what your code does 6 months from now, 2 years from now, 10 years?).

* - Unless some functions offer that, e.g. via plugins.