Burgershot
  • Home
  • Members
  • Team
  • Help
  • Search
  • Register
  • Login
  • Home
  • Members
  • Help
  • Search
Burgershot SA-MP Pawn Scripting [Pawn] Optimization in pawno.

Poll: Which one do you prefer to use?
You do not have permission to vote in this poll.
First code
100.00%
2 100.00%
Second code
0%
0 0%
Total 2 vote(s) 100%
* You voted for this item. [Show Results]

 
  • 0 Vote(s) - 0 Average
Pawn Optimization in pawno.
AsteriskRin
Offline

Burgershot Member
Posts: 1
Threads: 1
Joined: Dec 2020
Reputation: 0
#1
2020-12-03, 04:34 AM (This post was last modified: 2020-12-03, 04:36 AM by AsteriskRin.)
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);
    }
}
Awide
Offline

Burgershot Member
Posts: 62
Threads: 5
Joined: Sep 2019
Reputation: 5
#2
2020-12-03, 04:31 PM
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.
Desolation Roleplay has closed. You can download the gamemode here: https://www.burgershot.gg/showthread.php?tid=2272
Pinch
Offline

Burgershot Member
Posts: 391
Threads: 19
Joined: Apr 2019
Reputation: 22
Location: Belgrade, Serbia
#3
2020-12-03, 06:12 PM
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.
Kwarde
Offline

Burgershot Member
Posts: 99
Threads: 2
Joined: Sep 2020
Reputation: 8
Location: The Netherlands
#4
2020-12-04, 07:56 PM
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.
gzxmx94
Offline

Burgershot Member
Posts: 21
Threads: 2
Joined: Apr 2019
Reputation: 1
#5
2020-12-07, 10:12 AM (This post was last modified: 2020-12-07, 10:17 AM by gzxmx94.)
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.
« Next Oldest | Next Newest »



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Burgershot - Powered by our Community and MyBB Original Theme by Emerald

Linear Mode
Threaded Mode