Which one do you prefer to use?
First code
100.00%
2
Second code
0%
0
2 vote(s)
* You voted for this item. [Show Results]

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Optimization in pawno.
#1
Lightbulb 
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);

? ? }

}
  Reply
#2
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.
Check out Desolation Roleplay, where zombie AI and scavenging is bothered by player bandits!


  Reply
#3
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);

    }

}
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#4
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-Include...eatures.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-Asse...OCUMENT.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.
  Reply
#5
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.
  Reply


Forum Jump: