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.
#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


Messages In This Thread
Optimization in pawno. - by AsteriskRin - 2020-12-03, 04:34 AM
RE: Optimization in pawno. - by Awide - 2020-12-03, 04:31 PM
RE: Optimization in pawno. - by Pinch - 2020-12-03, 06:12 PM
RE: Optimization in pawno. - by Kwarde - 2020-12-04, 07:56 PM
RE: Optimization in pawno. - by gzxmx94 - 2020-12-07, 10:12 AM

Forum Jump: