foreach(new i: Player) { pInfo[i][pScore] += 1; VPUpdate(i, pScoreu); SetPlayerScore(i, pInfo[playerid][pScore]); }
for(new i = 0; i <= GetPlayerPoolSize(); i++) { pInfo[i][pScore] += 1; VPUpdate(i, pScoreu); SetPlayerScore(i, pInfo[playerid][pScore]); }
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)
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 ![]() Code 1: Код:
for(new i = 0;i <= GetPlayerPoolSize();i++) {} Код:
for(new i = 0,j = GetPlayerPoolSize();i <= j;i++) {} 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--) {} Код:
for(new i = GetPlayerPoolSize();--i != -1;) {} |