SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 20/06/2014, 10:40 AM   #1
Battlezone
Gangsta
 
Battlezone's Avatar
 
Join Date: Aug 2013
Location: Berlin
Posts: 805
Reputation: 162
Default Will looping inside a loop use more memory?

I got this code:

pawn Code:
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 Code:
foreach (new i : Player)
                {
                    if(BlabalTeam[TEST] == PlayerInfo[i][Team])
                    {
                        if(Demanded[i] == playerid)
                        {
                            ShowPlayerProgressBar(playerid, ZBar);
                            IsPlayerBlabla[playerid][TEST] = true;
                        }
                    }
                }
Battlezone is offline   Reply With Quote
Old 20/06/2014, 10:58 AM   #2
S4t3K
Gangsta
 
S4t3K's Avatar
 
Join Date: Jan 2013
Posts: 864
Reputation: 165
Default Re : Will looping inside a loop use more memory?

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.
__________________
S4t3K is offline   Reply With Quote
Old 20/06/2014, 11:08 AM   #3
Battlezone
Gangsta
 
Battlezone's Avatar
 
Join Date: Aug 2013
Location: Berlin
Posts: 805
Reputation: 162
Default Re: Will looping inside a loop use more memory?

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?
Battlezone is offline   Reply With Quote
Old 20/06/2014, 11:47 AM   #4
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: Will looping inside a loop use more memory?

16 kilobytes, woow. What year is it?
__________________
Vince is offline   Reply With Quote
Old 20/06/2014, 11:51 AM   #5
Pottus
High-roller
 
Pottus's Avatar
 
Join Date: Jun 2012
Posts: 4,885
Reputation: 1337
Default Re: Will looping inside a loop use more memory?

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.
Pottus is offline   Reply With Quote
Old 20/06/2014, 12:01 PM   #6
Battlezone
Gangsta
 
Battlezone's Avatar
 
Join Date: Aug 2013
Location: Berlin
Posts: 805
Reputation: 162
Default Re: Will looping inside a loop use more memory?

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?
Battlezone is offline   Reply With Quote
Old 20/06/2014, 12:27 PM   #7
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,122
Reputation: 529
Default Re: Will looping inside a loop use more memory?

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 Code:
if(!IsPlayerConnected(i)) continue;
__________________


Kar's Cops and Robbers 2020
Threshold is offline   Reply With Quote
Old 20/06/2014, 12:49 PM   #8
Battlezone
Gangsta
 
Battlezone's Avatar
 
Join Date: Aug 2013
Location: Berlin
Posts: 805
Reputation: 162
Default Re: Will looping inside a loop use more memory?

Im not using player loop, here is it

pawn Code:
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;
                            }
                        }
                    }
                }
            }
        }
    }
Battlezone is offline   Reply With Quote
Old 20/06/2014, 03:18 PM   #9
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,122
Reputation: 529
Default Re: Will looping inside a loop use more memory?

pawn Code:
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.
__________________


Kar's Cops and Robbers 2020
Threshold is offline   Reply With Quote
Old 20/06/2014, 05:59 PM   #10
Battlezone
Gangsta
 
Battlezone's Avatar
 
Join Date: Aug 2013
Location: Berlin
Posts: 805
Reputation: 162
Default Re: Will looping inside a loop use more memory?

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

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
loop inside of loop? newbienoob Scripting Help 3 17/12/2013 04:06 AM
Writing to SQLite inside loop CJay9209 Scripting Help 5 15/04/2013 03:40 AM
Help with loop inside loop Salim_Karaja Scripting Help 4 24/02/2013 07:02 PM
using CreateObject function inside a loop deffo Scripting Help 4 28/12/2012 01:31 PM
How to stop a for/while loop looping after it founds the first result? DJ-CJ89 Help Archive 4 15/04/2010 11:20 AM


All times are GMT. The time now is 05:55 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.