Raised This Month: $ Target: $400
 0% 

Better way to declare in loops


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-24-2016 , 15:38   Better way to declare in loops
Reply With Quote #1

Hey guys, I've been hearing a lot that I should declare variable outside the loop for example:
PHP Code:
    int value;

    for (
int iterator 99999iterator 0iterator--)
    {
         
value 1000;

         
/* Do something */    
    

Instead of doing:
PHP Code:
    for (int iterator 99999iterator 0iterator--)
    {
         
int value 1000;

         
/* Do something */    
    

Well in C++ some said that C++ compilers are smart enough to optimize it for you, but does it happen with sourcemod compiler ?
TheDS1337 is offline
thecount
Veteran Member
Join Date: Jul 2013
Old 01-24-2016 , 16:05   Re: Better way to declare in loops
Reply With Quote #2

Good practice to assume it doesn't.
thecount is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 01-24-2016 , 17:44   Re: Better way to declare in loops
Reply With Quote #3

You could compile it and then run it through the lysis online decompiler. Spcomp also has an -a flag that you might be interested in.
__________________

Last edited by Impact123; 01-24-2016 at 17:48.
Impact123 is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-24-2016 , 17:50   Re: Better way to declare in loops
Reply With Quote #4

Nice idea, I'll try that tomorrow, Good night for now.
TheDS1337 is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 01-24-2016 , 18:14   Re: Better way to declare in loops
Reply With Quote #5

AFAIK, the variable is "allocated" at compile time (on stack or heap), thus both code should give the same binary.
h3bus is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 01-24-2016 , 20:10   Re: Better way to declare in loops
Reply With Quote #6

Quote:
Originally Posted by h3bus View Post
AFAIK, the variable is "allocated" at compile time (on stack or heap), thus both code should give the same binary.
Global/static variables are indeed allocated at compile time, but local variables aren't. Stack allocation isn't really allocation actually (the plugin won't take more memory), only the stack pointer is moved, and that stack pointer moving is an additional instruction. Memory isn't the concern here, it's the execution time.

Last edited by klippy; 01-24-2016 at 20:11.
klippy is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 01-25-2016 , 02:07   Re: Better way to declare in loops
Reply With Quote #7

Quote:
Originally Posted by KliPPy View Post
Global/static variables are indeed allocated at compile time, but local variables aren't. Stack allocation isn't really allocation actually (the plugin won't take more memory), only the stack pointer is moved, and that stack pointer moving is an additional instruction. Memory isn't the concern here, it's the execution time.
That's why I put the quote around "allocated". My point that there is little chance that stack pointer is pushed up and down at each loop as wherever that variable is in the code, its displacement relative to stack/heap pointer is most likely static after compile time.
h3bus is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-25-2016 , 01:20   Re: Better way to declare in loops
Reply With Quote #8

Declaring the variable outside the "for" block:
PHP Code:
public void:OnPluginStart()
{
    new 
value;
    new 
i;
    while (
100)
    {
        
value 1000000000;
        
i++;
    }
    return 
void:0;

Inside it:
PHP Code:
public void:OnPluginStart()
{
    new 
i;
    while (
100)
    {
        
i++;
    }
    return 
void:0;

TheDS1337 is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 01-25-2016 , 04:54   Re: Better way to declare in loops
Reply With Quote #9

Quote:
Originally Posted by DeagLe.Studio View Post
Declaring the variable outside the "for" block:

...
You need to do something with the variable so it won't optimize it out
Miu is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-25-2016 , 06:37   Re: Better way to declare in loops
Reply With Quote #10

Quote:
Originally Posted by Miu View Post
You need to do something with the variable so it won't optimize it out
Oh, I didnt know that.

But anyways guys, I always declare vars outside the loop. the thing is that I love to choose a good coding style if you know what I mean, and recently I saw a lot of people declaring the vars inside the loop I thought it'll be cool to do it (but only if compiler actually optimize it).
TheDS1337 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 08:50.


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