[Tutorial] Speed test.
#1

Well, in this tutorial I will teach them to how doing speed tests in our codes.
The first thing that we must know is what we are going to use:


  • A variable:
    • With her we will store values with....
  • GetTickCount:
    • In him the variables will be stored. (GetTickCount returns values in milliseconds).

Probably wonder: And how is it possible to do speed tests in the codes? The response is simple: SA-MP runs in an alone thread, with this I refer that the actions execute 1 for 1. Or, an action is executed by a certain moment, on having finished this moment the following one is executed and so on.

Let's see the following example:


pawn Код:
public OnFilterScriptInit()
{
    new count = GetTickCount();
    for(new i = 0; i < 50; i++)
    {
        SendRconCommand("gmx");
    }
    printf("Time in executing: %i.", GetTickCount()-count);
    return 1;
}

Do this test of speed and compare the results with this one:


pawn Код:
public OnFilterScriptInit()
{
    new count = GetTickCount();
    for(new i = 0; i < 10; i++)
    {
        SendRconCommand("gmx");
    }
    printf("Time in executing: %i.", GetTickCount()-count);
    return 1;
}

They will notice that the result is very much minor that the previous one.

These are my results:


pawn Код:
// Loop of 50 repetitions:
[23:03:50] Time in executing: 73.
[23:03:55] Time in executing: 86.
[23:04:02] Time in executing: 80.
// Rounding: 79


// Loop of 10 repetitions:
[23:09:16] Time in executing: 18.
[23:09:21] Time in executing: 22.
[23:09:29] Time in executing: 17.
// Rounding: 19.
Do they see the notable thing that is the difference?
Now already we know how to do the speed tests.

Here another example: using a normal loop and foreach:



pawn Код:
// Using a normal loop:

CMD:test(playerid, params[])
{
    new count = GetTickCount();
    new string[60];
    new Nick[MAX_PLAYER_NAME];
    GetPlayerName(playerid, Nick, sizeof(Nick));
    format(string, sizeof(string), "* Your principal information: %s[%d].", Nick, playerid);
    for(new i = 0; i < GetMaxPlayers(); i++)
    {
        if(IsPlayerAdmin(i))
        {
            SendClientMessage(i, -1, string);
        }
    }
    printf("Time in executing: %i.", GetTickCount()-count);
    return 1;
}


// Using foreach:

CMD:test2(playerid, params[])
{
    new count = GetTickCount();
    new string[60];
    new Nick[MAX_PLAYER_NAME];
    GetPlayerName(playerid, Nick, sizeof(Nick));
    format(string, sizeof(string), "* Your principal information: %s[%d].", Nick, playerid);
    foreach(new i: Player)
    {
        if(IsPlayerAdmin(i))
        {
            SendClientMessage(i, -1, string);
        }
    }
    printf("Time in executing: %i.", GetTickCount()-count);
    return 1;
}

My results:


pawn Код:
[23:52:04] Time in executing (normal loop): 75.
[23:52:16] Time in executing (normal loop): 83.
[23:52:17] Time in executing (normal loop): 87.

[23:52:18] Time in executing (foreach): 22.
[23:52:19] Time in executing (foreach): 26.
[23:52:20] Time in executing (foreach): 17.
Note: I used '500' slot's to differentiate well the results.

I hope that it has served them.
If have some doubt, or I have been wrong in something make it know.


Regards.
Reply
#2

The more loops, the more ms/s GTC prints.

Try using GetTickCount with a looped "gmx" and an unlooped "gmx"

You should also mention, when you use foreach to loop every player with a command, it lags more if there are more players, this should be in the thread as a conclusion

Although you didn't explain anything. What's the "-count"? Why do I have to define a variable as GetTickCount and use the same thing with GetTickCount again, this time by difference? (Could also me GetTickCount - GetTickCount, but why not?)
Reply
#3

Sorry if the topic is incomplete, I will try to explain more things.


Quote:
Originally Posted by Sublime
Посмотреть сообщение
The more loops, the more ms/s GTC prints.

Try using GetTickCount with a looped "gmx" and an unlooped "gmx"

You should also mention, when you use foreach to loop every player with a command, it lags more if there are more players, this should be in the thread as a conclusion

Although you didn't explain anything. What's the "-count"? Why do I have to define a variable as GetTickCount and use the same thing with GetTickCount again, this time by difference? (Could also me GetTickCount - GetTickCount, but why not?)
Yes, but not always we be be sure of what ID it is connected and what ID is not. Foreach only works for the connected ID's. On why it is necessary to store the variables, it is for that, as charm, SA-MP runs in an alone thread and every thing goes his time.

Then, when 'X' action is begun to execute, we create a variable and store it obtaining the time (GetTickCount), and on having finished this action, we format another not stored GetTickCount and reduce his value with the stored variable.

If we were reducing 'GetTickCount' with another 'GetTickCount', the value always would be: 0.

Sorry my bad english.

Regards.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)