SA-MP Forums Archive
Will looping inside a loop use more memory? - 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: Will looping inside a loop use more memory? (/showthread.php?tid=520789)



Will looping inside a loop use more memory? - Battlezone - 20.06.2014

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;
                        }
                    }
                }



Re : Will looping inside a loop use more memory? - S4t3K - 20.06.2014

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.


Re: Will looping inside a loop use more memory? - Battlezone - 20.06.2014

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?


Re: Will looping inside a loop use more memory? - Vince - 20.06.2014

16 kilobytes, woow. What year is it?


Re: Will looping inside a loop use more memory? - Pottus - 20.06.2014

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.


Re: Will looping inside a loop use more memory? - Battlezone - 20.06.2014

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?


Re: Will looping inside a loop use more memory? - Threshold - 20.06.2014

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;



Re: Will looping inside a loop use more memory? - Battlezone - 20.06.2014

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;
                            }
                        }
                    }
                }
            }
        }
    }



Re: Will looping inside a loop use more memory? - Threshold - 20.06.2014

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.


Re: Will looping inside a loop use more memory? - Battlezone - 20.06.2014

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