Raised This Month: $32 Target: $400
 8% 

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
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-25-2016 , 01:20   Re: Better way to declare in loops
Reply With Quote #7

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
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 01-25-2016 , 01:49   Re: Better way to declare in loops
Reply With Quote #8

It doesn't look like the variable even made it into the code in the second snippet at all...
__________________
ddhoward 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 #9

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
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-25-2016 , 02:16   Re: Better way to declare in loops
Reply With Quote #10

They probably do optimize that (can't be bothered to check the C++ source) since the JIT does a more or less 1-to-1 mapping of SM bytecode to assembly/machine code.

It's better to assume that the compiler is dumb and does a simple 1-to-1 mapping of pawn to bytecode with no optimizations in place. Having said that, you should only declare things outside of loop if the loop is to be ran an insane number of times (insane being relative, of course). If the loop is only ran several times (on the order of magnitude of 100), depending on complexity it's sometimes better to just leave the declaration in the loop. Otherwise you may introduce subtle bugs in your plugin just for a 0.0000001% speedup or something (premature optimization trap).

Realistically speaking though, integer declarations are pretty much negligible. It's only sizable string or array declarations that you should be looking at moving outside of loops or to global scope.

Last edited by Potato Uno; 01-25-2016 at 02:16.
Potato Uno 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 16:52.


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