Raised This Month: $51 Target: $400
 12% 

[FAQ] Whether to create variables inside/outside loops


Post New Thread Reply   
 
Thread Tools Display Modes
GuskiS
Veteran Member
Join Date: Aug 2007
Location: Latvia
Old 11-04-2014 , 12:46   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #11

Quote:
Originally Posted by HamletEagle View Post
Also your example is wrong, you are using the iterator i that is never declared.
for(new var, i; i < x; i++ )
It is defined, same as var
PHP Code:
// You example:
public func()
{
    new var;
    for(new 
i15i++)
    {
        var++;
    }

    
log_amx("Var value is %d", var); // prints out 14
}

// My and Arkshine's example:
public func()
{
    for(new var, 
i15i++)
    {
        var++;
    }

    
log_amx("Var value is %d", var); // Give error on compile cause var isn't declared.

Look carefully and you will see the difference. It is not a bad way, but since you gave example where you use var only inside a loop, you should know you can use this way.

Quote:
Originally Posted by Arkshine View Post
Q1. Defining a scope for used variables is welcome. In your example you can do that easily:
It's usually a good idea to declare variables near where you use them.
__________________
Finished mods:
Trouble in Terrorist Town
MurderMod
The Hidden
Cowboys vs Indians
JailBreak Supreme
Survival Madness
GuskiS is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-04-2014 , 12:49   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #12

I know it, but I think I'm blind, didn't saw ",i", my bad.
__________________

Last edited by HamletEagle; 11-04-2014 at 12:50.
HamletEagle is offline
ezio_auditore
Senior Member
Join Date: May 2013
Old 11-05-2014 , 00:35   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #13

Quote:
Originally Posted by GuskiS View Post
You are saying that creating variable takes 4 bytes and asigning it with new value also take 4 bytes, but if I leave other untouched?
Declaration takes nothing but if you assign the variable, then it will hold the reference of the of the 4 bytes representing the value you assigned the variable with. And if you assign the same variable with a different value, it will then hold the reference of the another 4 bytes representing the new value. The same occurs even if you declare the variable outside or inside.
__________________
ezio_auditore is offline
Send a message via Skype™ to ezio_auditore
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 11-05-2014 , 05:49   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #14

Why to argue about this? Correct way is to create outside. It does not matters if you can create inside of loop sometimes, as hamlet said, begginers will get confused if you tell them this. They will think they can create inside the loops everytime and they will do things wrong. Also, when you can create some inside, it doesnt matters if you create outside. So, this thread seem a bit useless, it should always be created outside, no matter if can be created inside.
__________________
Jhob94 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-05-2014 , 09:16   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #15

Quote:
Originally Posted by ezio_auditore View Post
Declaration takes nothing but if you assign the variable, then it will hold the reference of the of the 4 bytes representing the value you assigned the variable with. And if you assign the same variable with a different value, it will then hold the reference of the another 4 bytes representing the new value. The same occurs even if you declare the variable outside or inside.
Assigning does not use any memory, declaration does, as it pushes the stack, allocating 1 cell(4 or 8 bytes) on top of it. According to that, this code:
Code:
new x; // Declaring a new variable
translates to:
Code:
push.c	0
meaning that it allocates(pushes) 1 cell onto the stack.
Assigning takes no memory, so this code:
Code:
x = 5;
will translate to:
Code:
const.pri	5
stor.s.pri	[offset in bytes from the stack frame pointer to variable "x"]
which, as you can see, has nothing to do with stack or heap, meaning that it takes no memory.
And this variable doesn't hold any reference to 4 bytes or whatever, it always has the same memory location while it is allocated, the only thing that changes is the value of the allocated 4 bytes.

And the interesting thing is, which I've just found out while experimenting, that this:
Code:
new x = 5; /* push.c  5 */
is faster than this is:
Code:
new x; x = 5; /* push.c  0 const.pri   5 stor.s.pri  [offset in bytes from the stack frame pointer to variable "x"] */

for obvious reason. Another sign that PAWN compiler can't optimize code.
klippy is offline
Jhob94
AMX Mod X Donor
Join Date: Jul 2012
Old 11-05-2014 , 09:44   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #16

Quote:
Originally Posted by KliPPy View Post
And the interesting thing is, which I've just found out while experimenting, that this:
Code:
new x = 5; /* push.c  5 */
is faster than this is:
Code:
new x; x = 5; /* push.c  0 const.pri   5 stor.s.pri  [offset in bytes from the stack frame pointer to variable "x"] */

for obvious reason. Another sign that PAWN compiler can't optimize code.
This part i didn't knew. Indeed interesting. Thanks for share
__________________
Jhob94 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-05-2014 , 10:34   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #17

@KliPPy, did you get this infos by decompiling ?
__________________
HamletEagle is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 11-05-2014 , 10:38   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #18

Disassembling. (amxxdump for example)
__________________
Arkshine is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-05-2014 , 15:09   Re: [FAQ] Whether to create variables inside/outside loops
Reply With Quote #19

Quote:
Originally Posted by Arkshine View Post
Disassembling. (amxxdump for example)
Exactly, I used amxxdump.

Quote:
Originally Posted by HamletEagle View Post
@KliPPy, did you get this infos by decompiling ?
The only thing I found out by disassembling is the experiment I mentioned in the last post. Everything I said before that are things I've known already, and I do understand Pawn op-codes.
klippy is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 04:47.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode