SA-MP Forums Archive
Which one is fastest loop? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Which one is fastest loop? (/showthread.php?tid=493936)



Which one is fastest loop? - AA9 - 10.02.2014

I want to know, which one is faster.

pawn Код:
for (new i; i < 1000000; i++)
    {

        if(PlayerInfo[i][pMember] == 4)
        {
                SendClientMessage(playerid.....
                 }
      }
pawn Код:
for (new i; i < 10000000; i++)
    {
    if(PlayerInfo[i][pMember] != 4) continue;

     SendClientMessage(playerid.........
    }



Re: Which one is fastest loop? - Shockey HD - 10.02.2014

Whats the point of looping that many times?


Regardless,

pawn Код:
for (new i; i < 10000000; i++)
    {
    if(PlayerInfo[i][pMember] != 4) continue;

     SendClientMessage(playerid.........
    }

I guess that would be faster, as you wouldnt be loading everything after continue..


Re: Which one is fastest loop? - AA9 - 10.02.2014

Just put some random value there.


Re: Which one is fastest loop? - Shockey HD - 10.02.2014

For players just use MAX_PLAYERS lol


Re: Which one is fastest loop? - Michael@Belgium - 10.02.2014

Quote:
Originally Posted by Shockey HD
Посмотреть сообщение
For players just use MAX_PLAYERS lol
GetMaxPlayers() is even better.


Re: Which one is fastest loop? - Konstantinos - 10.02.2014

The difference on them about the time they take to be executed is not something you should worry about!

Quote:
Originally Posted by Shockey HD
Посмотреть сообщение
Whats the point of looping that many times?
Benchmarking.

Quote:
Originally Posted by Shockey HD
Посмотреть сообщение
I guess that would be faster, as you wouldnt be loading everything after continue..
You're actually wrong. It executes an if statement, if the statement is true it goes on continue else in the code above.

The first one (without the use of continue) executes the if statement and if it's true, it executes the code directly and it doesn't have any other case so that makes it faster.

Quote:
Originally Posted by Michael@Belgium
Посмотреть сообщение
GetMaxPlayers() is even better.
And why is that? It executes a function while MAX_PLAYERS is a const number.


Re: Which one is fastest loop? - AA9 - 10.02.2014

Im not stupid, i dont use it as part of my code. I writed a code, to test loop speed, because with small loop value you dont see difference. But i had to go and i posted part of the code here.


Re: Which one is fastest loop? - Patrick - 10.02.2014

Quote:
Originally Posted by AA9
Посмотреть сообщение
Im not stupid, i dont use it as part of my code. I writed a code, to test loop speed, because with small loop value you dont see difference. But i had to go and i posted part of the code here.
I have done some speedtest, using 100,000 iterations out of 500,000 sized array, I'd say using continue is much faster than using if statement. Here are some result and the code I used, you can test this code using Slice Pawn Playground | Result + Code: Source

NOTE: 500,000 sized array are not meant to be used! this is just an example code to prove which one of these code are faster.


Re: Which one is fastest loop? - PowerPC603 - 10.02.2014

The size of your array doesn't matter in that code.
Even if you increase that to 2 billion, it doesn't matter because you're only using the first 100000 indices of it with the ITERATIONS value.

And the result is negligable.
Maximum 2 ms difference for 100000 iterations.


Re: Which one is fastest loop? - Patrick - 10.02.2014

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
The size of your array doesn't matter in that code.
Even if you increase that to 2 billion, it doesn't matter because you're only using the first 100000 indices of it with the ITERATIONS value.

And the result is negligable.
Maximum 2 ms difference for 100000 iterations.
That is true, the maximum difference I've got is 2 milliseconds

pawn Код:
/* Results
 
1. Testing Started
 
Without continue took 8 ms to execute 100000 iterations
With continue 7 ms to execute 100000 iterations
 
Testing Ended
 
2. Testing Started
 
Without continue took 10 ms to execute 100000 iterations
With continue 8 ms to execute 100000 iterations
 
Testing Ended
 
3. Testing Started
 
Without continue took 7 ms to execute 100000 iterations
With continue 7 ms to execute 100000 iterations
 
Testing Ended
 
4. Testing Started
 
Without continue took 8 ms to execute 100000 iterations
With continue 7 ms to execute 100000 iterations
 
Testing Ended
 
*/