AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Creating static inside loop (https://forums.alliedmods.net/showthread.php?t=242091)

Black Rose 06-13-2014 18:22

Creating static inside loop
 
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.

Flick3rR 06-13-2014 18:33

Re: Creating static inside loop
 
Second.
Quote:

Originally Posted by hornet (Post 2146566)
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.

fysiks 06-13-2014 19:45

Re: Creating static inside loop
 
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.

claudiuhks 06-13-2014 19:48

Re: Creating static inside loop
 
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



Black Rose 06-13-2014 20:16

Re: Creating static inside loop
 
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.

fysiks 06-13-2014 20:39

Re: Creating static inside loop
 
I wouldn't worry about it since you shouldn't ever do it.

Nextra 06-13-2014 20:50

Re: Creating static inside loop
 
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.

Black Rose 06-13-2014 20:58

Re: Creating static inside loop
 
Thank you.

hleV 06-13-2014 21:09

Re: Creating static inside loop
 
Quote:

Originally Posted by fysiks (Post 2151308)
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.


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

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