Raised This Month: $ Target: $400
 0% 

Creating static inside loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 06-13-2014 , 18:22   Creating static inside loop
Reply With Quote #1

Code:
for ( new i ; i < gSayCmdCount ; i++ ) {     static TempString2[32]; }
vs.
Code:
static TempString2[32]; for ( new i ; i < gSayCmdCount ; i++ ) { }
Is there a difference or will it only do stuff the first time, when it is initialized?
I thought about using this to prevent wasting that (32 CELLS!!!! of) memory unless there actually is a count. But if it comes at the cost of CPU load it's useless.
__________________
Black Rose is offline
Flick3rR
Veteran Member
Join Date: Feb 2014
Location: Bulgaria, Stara Zagora
Old 06-13-2014 , 18:33   Re: Creating static inside loop
Reply With Quote #2

Second.
Quote:
Originally Posted by hornet View Post
Never create a new variable inside a loop.
https://forums.alliedmods.net/showth...highlight=loop
I don't know if this also аpplies to statics, but I think it's something sort of.
__________________
Flick3rR is offline
Send a message via Skype™ to Flick3rR
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-13-2014 , 19:45   Re: Creating static inside loop
Reply With Quote #3

It's essentially the same thing except for scope. Generally, if you create the variable inside the loop, it cannot be used outside the loop. The fact that it is static will mean there won't be any significant performance degradation after it has run once. Regardless, it always best to stick to the "never create variables inside a loop" recommendation.
__________________
fysiks is offline
claudiuhks
Yam Inside®™℠
Join Date: Jan 2010
Location: Living Randomly
Old 06-13-2014 , 19:48   Re: Creating static inside loop
Reply With Quote #4

I suggest static type of variables to be declared at the beginning of a function that executes really frequently.

PHP Code:
{typeexecuteMeFrequently()
{
  static {
type} {name}[{count}];

  
// do anything and return data if needed

__________________

Last edited by claudiuhks; 06-13-2014 at 19:50.
claudiuhks is offline
Send a message via MSN to claudiuhks Send a message via Yahoo to claudiuhks Send a message via Skype™ to claudiuhks
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 06-13-2014 , 20:16   Re: Creating static inside loop
Reply With Quote #5

Let me be more specific. I understand the scope and I know that the memory allocation for a static is not released until the plugin is unloaded. That is not the question.

Is there some kind of function that checks if the memory has been allocated that will run every time i try to create the static inside of the loop? Will this create extra work during runtime at each loop execution that should be avoided?

I'm not using it either way. It will only make things more complicated. Now it's just curiosity.
__________________
Black Rose is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-13-2014 , 20:39   Re: Creating static inside loop
Reply With Quote #6

I wouldn't worry about it since you shouldn't ever do it.
__________________
fysiks is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-13-2014 , 20:50   Re: Creating static inside loop
Reply With Quote #7

Static variables are stored in the DAT section of the plugin and are present for the entirety of the plugin's lifetime, which means that allocation happens automatically with loading your plugin. It should be obvious by their behavior that, even though they are scoped in the code syntax and the compiler enforces this, they technically are not local to the function. They work exactly the same as global variables in this respect - you just can't access them the same way through code.
__________________
In Flames we trust!

Last edited by Nextra; 06-13-2014 at 20:51.
Nextra is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Upplands Väsby, Sweden
Old 06-13-2014 , 20:58   Re: Creating static inside loop
Reply With Quote #8

Thank you.
__________________
Black Rose is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 06-13-2014 , 21:09   Re: Creating static inside loop
Reply With Quote #9

Quote:
Originally Posted by fysiks View Post
it always best to stick to the "never create variables inside a loop" recommendation.
Variables should always be declared as close to the scope they're used in as possible, unless doing so reduces performance, which is not the case here. So leaving the static variable declaration inside the loop is the right thing to do.
__________________
hleV is offline
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

Forum Jump


All times are GMT -4. The time now is 02:11.


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