How is it right?
#1

How is that right? Option 1 or Option 2?

Option 1:
Код HTML:
foreach(new i: Player)
{
    pInfo[i][pScore] += 1; VPUpdate(i, pScoreu);
    SetPlayerScore(i, pInfo[playerid][pScore]);
}
Option 2:
Код HTML:
for(new i = 0; i <= GetPlayerPoolSize(); i++)
{
    pInfo[i][pScore] += 1; VPUpdate(i, pScoreu);
    SetPlayerScore(i, pInfo[playerid][pScore]);
}
Reply
#2

both are right, first one is faster
and second one is kinda badly optimized, I would suggest you use first one for players, learn optimized version of second one for other loops a simple gamemode will have many many loops
Reply
#3

Option 1 uses the foreach include made by ****** and option 2 uses the GetPlayerPoolSize function implemented by the SA:MP team. Both are correct. Though, option 2 can easily be optimised as follows:

PHP код:
for(new 0GetPlayerPoolSize(); <= j++) 
The explanation behind it:
Quote:
Originally Posted by Yashas
Посмотреть сообщение
4. Conditions in loops

I don't know how many times I have told people about this but still there are few (I think there are many ) who still don't do this simple optimization.

Code 1:
Код:
for(new i = 0;i <= GetPlayerPoolSize();i++) {}
Code 2:
Код:
for(new i = 0,j = GetPlayerPoolSize();i <= j;i++) {}
Code 2 will perform better.

Why?
In code1, GetPlayerPoolSize is called for every iteration.But GetPlayerPoolSize returns a fixed number in our context.Then why call it again and again?

Yes, that's what the code 2 avoids.It makes a local variable which stores the value returned by GetPlayerPoolSize and uses it in the condition.Therefore calling the function just once and avoiding the function overhead.

The same code can be optimized further
Код:
for(new i = GetPlayerPoolSize();i != -1;i--) {}
Use the above if the order in which the code is executed does not matter. You can optimize that too further but I feel that it will reduce the readability of the code.

Код:
for(new i = GetPlayerPoolSize();--i != -1;) {}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)