Will looping inside a loop use more memory?
#1

I got this code:

pawn Код:
if(checkpointid == CP[TEST])
    {
        if(Balblabl[TEST] == false)
        {
            if(tCP[TEST] != PlayerInfo[playerid][Team])
            {
                Blabal(playerid, TEST);
            } else return SendClientMessage(playerid, COLOR_RED,"*Blabla taken!");
        }
        if(UnderAttack[TEST] == true)
        {
            if(BlabalTeam[TEST] == PlayerInfo[playerid][Team] && IsPlayerBlabla[playerid][TEST] == false)
            {
                ShowPlayerProgressBar(playerid, ZBar);
                IsPlayerBlabla[playerid][TEST] = true;
            }
            if(BlabalTeam[TEST] != PlayerInfo[playerid][Team])
            {
                foreach (new i : Player)
                {
                    if(BlabalTeam[TEST] == PlayerInfo[i][Team])
                    {
                        if(Demanded[i] == playerid)
                        {
                            ShowPlayerProgressBar(playerid, ZBar);
                            IsPlayerBlabla[playerid][TEST] = true;
                        }
                    }
                }
            }
        }
    }

This code is not only used by [TEST] , but it repeats 36 times with different zones so either I make this code for each zone every time, or I simply use a loop for all zones, but Im afraid it will use more memory because there is already a loop , this one :

pawn Код:
foreach (new i : Player)
                {
                    if(BlabalTeam[TEST] == PlayerInfo[i][Team])
                    {
                        if(Demanded[i] == playerid)
                        {
                            ShowPlayerProgressBar(playerid, ZBar);
                            IsPlayerBlabla[playerid][TEST] = true;
                        }
                    }
                }
Reply
#2

Using a loop uses more memory than using nothing.
Using a loop inside a loop will logically use more memory than using a single loop.

It's common sense.
Reply
#3

But I think it's the same here, because this code will be repeated for each zone, therefore it will be like looping them.
EDIT: I tried the two methods, when I used loops, my amx file size has decreased by 16 kb, what do u think?
Reply
#4

16 kilobytes, woow. What year is it?
Reply
#5

Really depends on what your doing, usually for most purposes you can get away from using a loop in a loop but you didn't even supply sufficient or meaningful code to make any determination of what your trying to do in order to choose the best way to do it memory is not an issue.
Reply
#6

I can't really understand guys.. that code repeats 36 times under OnPlayerDisconnect, OnPlayerDeath and OnPlayerLeaveDynamicCP, which means 108 times in total, i changed them to loops, and now the file size is 183 kb after it was 207 kb.
Any explanation? shall i roll back changes?
EDIT: Which is better? using if( with all variables or using a loop?
Reply
#7

EDIT: Let me just rethink what I was meant to say...

EDIT #2: Well if you're going to go through all 'ONLINE' player, then you have to use a loop.

If you did:
var[0] = 1;
var[1] = 1;

You can be setting a non-existent player's stuff to 1. So loops can help you cut out what you don't need like:
pawn Код:
if(!IsPlayerConnected(i)) continue;
Reply
#8

Im not using player loop, here is it

pawn Код:
for(new i2 = 0; i2 < 36; ++i2)
    {
        if(checkpointid == CP[i2])
        {
            if(Blabal[i2] == false)
            {
                if(tCP[i2] != PlayerInfo[playerid][Team])
                {
                    ActiveBla(playerid, i2);
                } else return SendClientMessage(playerid, COLOR_RED,"*Blabla taken!");
            }
            if(Blabal[i2] == true)
            {
                if(BLablaTeam[i2] == PlayerInfo[playerid][Team] && IsPlayerBlabla[playerid][i2] == false)
                {
                    ShowPlayerProgressBar(playerid, ZBar);
                    IsPlayerBL[playerid][i2] = true;
                }
                if(BLablaTeam[i2] != PlayerInfo[playerid][Team])
                {
                    foreach (new i : Player)
                    {
                        if(BLablaTeam[i2] == PlayerInfo[i][Team])
                        {
                            if(Demanded[i] == playerid)
                            {
                                ShowPlayerProgressBar(playerid, ZBar);
                                IsPlayerBL[playerid][i2] = true;
                            }
                        }
                    }
                }
            }
        }
    }
Reply
#9

pawn Код:
for(new j = 0; j < 36; ++j)
    {
        if(checkpointid == CP[j])
        {
            if(!Blabal[j])
            {
                if(tCP[j] == PlayerInfo[playerid][Team]) return SendClientMessage(playerid, COLOR_RED,"*Blabla taken!");
                ActiveBla(playerid, j);
                break;
            }
            else
            {
                if(BLablaTeam[j] == PlayerInfo[playerid][Team])
                {
                    if(!IsPlayerBlabla[playerid][j])
                    {
                        ShowPlayerProgressBar(playerid, ZBar);
                        IsPlayerBL[playerid][j] = true;
                        break;
                    }
                }
                else
                {
                    new bool:var = false;
                    foreach(new i : Player)
                    {
                        if(BLablaTeam[j] == PlayerInfo[i][Team])
                        {
                            if(Demanded[i] == playerid)
                            {
                                ShowPlayerProgressBar(playerid, ZBar);
                                IsPlayerBL[playerid][j] = true;
                                var = true;
                                break;
                            }
                        }
                    }
                    if(var) break;
                }
            }
        }
    }
Foreach(new i : Player) is a player loop.
Reply
#10

Will this optimize the code?
EDIT: new bool:var = false;
This bool will be created for each player who enter the checkpoint, right?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)