[Tutorial] How to make the fastest plain player-loop
#1

Hello there.
I've noticed that in this theard Slice said, that this is the fastest players loop:
pawn Код:
for ( new slots = GetMaxPlayers( ), i; i < slots; i++ )
{
    if ( !IsPlayerConnected( i ) )
        continue;
   
    // code for connected players
}
But actually it isn't, because the decrement in PAWN is more faster that increment and the fastest players loop is:
pawn Код:
for(new i=GetMaxPlayers()-1; i != -1; i--)
{
    if !IsPlayerConnected(i) *then continue;

}
Proofs:

pawn Код:
main()
{
    new tick = GetTickCount();
    for ( new slots = getMaxPlayers( ), i; i < slots; i++ )
    {}
    printf("- %d", GetTickCount()-tick);
    tick = GetTickCount();
    for(new i=getMaxPlayers(); i != -1; i-- )
    {}
    printf("- %d", GetTickCount()-tick);
    return 0;
}

getMaxPlayers() return 10000000;
Quote:

- 378
- 397

I know that with a small number of iterations it doesn't matter, but in the cases, when you're making loop with the vehicles (which limit is 2000) or the large strings it's really works faster.

Thank you for reading it and sorry for the mistakes.
Reply
#2

Thanks .
Reply
#3

First of all, you should use pawn tags for Pawn code.

About the fact that decrementating is faster. Well maybe, but it still woudn't affect the time it takes to execute the code within the loop.

And I ran both incrementing and decrementing loops 1 000 000 and the results vary in each run. Try running it multiple times and see if its still faster.
Reply
#4

This is nice. Waiting for other people suggestions.
Reply
#5

What's wrong with
pawn Код:
#include <a_samp>
#undef MAX_PLAYERS
#define MAX_PLAYERS (desired server slots)

for (new i = 0; i < MAX_PLAYERS; i++)
{

}
?
Reply
#6

I've tried it with a 200000 loop, both gave the same amount of ms.
Код:
for (new slots = 200000, i; i < slots; i++) : 4
for (new i = 200000; i != -1; i--) : 4
I increased it to 2000000, yours was faster by 2-3 ms.

Who the hell does something 200000 times?
Reply
#7

Quote:
Originally Posted by Mellnik
Посмотреть сообщение
What's wrong with
pawn Код:
#include <a_samp>
#undef MAX_PLAYERS
#define MAX_PLAYERS (desired server slots)

for (new i = 0; i < MAX_PLAYERS; i++)
{

}
?
It isn't too faster, but #define is better with MAX_PLAYERS variable.

Quote:

Who the hell does something 200000 times?

Doesn't matter, fact is fact.
Reply
#8

how about foreach
Reply
#9

Quote:
Originally Posted by dusk
Посмотреть сообщение
First of all, you should use pawn tags for Pawn code.
Sorry, I didn't know about this tag.

Quote:
Originally Posted by dusk
Посмотреть сообщение
About the fact that decrementating is faster. Well maybe, but it still woudn't affect the time it takes to execute the code within the loop.
Well, the difference in milliseconds or nanoseconds but the difference actually is and it affect to the code in generally/

Quote:

And I ran both incrementing and decrementing loops 1 000 000 and the results vary in each run. Try running it multiple times and see if its still faster.

the 1st time:

Quote:

- 53
- 40

the 2nd:
Quote:

- 54
- 39

the 3th:
Quote:

- 39
- 38

So, I've tested it on PPG (http://slice-vps.nl:7070/).
Maybe, someone'll test it too?


This tutorial haven't created in order to humiliate someone, but in order to pass on my knowledge to others.
Reply
#10

What's wrong with foreach?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)