Quote:
Originally Posted by thEsp
By the way, you are creating a new variable in each client pre-think forward, which is totally not good, so you should make it static.
|
Meh, not necessarily. I don't have anything against using static in this case, but it's not going to make much of a difference because we are talking about a variable, not an array.
Here's what happens:
Variables created with "new" are placed on the stack. Every time a function is called it will have to:
1. create that variable: allocate space for it on the stack, which is done with a simple subtraction operation(I don't want to go into details about why subtraction is done, I explained in another post somewhere and you can read about how variables are allocated on the stack if you need details). Basically it's a single CPU instruction, doesn't create any overhead.
2. in a language like pawn where variables are initialized with 0, the code will have to 0 the memory location where the variable is. For a single cell variable, it only has to set 4 bytes of data to 0, again no overhead.
3. destroy the variable when the function is done to clean the stack: this is done with an add operation(basically the opposite of step 1). No overhead for a single add CPU instruction.
Variables created with "static" are not placed on the stack, they go in the "data" section. The function no longer needs to do steps 1 and 3 since the variable is not stored in a section that needs constant allocation/cleaning for variables(like the stack). It is placed in the data section and it stays there for the duration of the execution.
But again, not having to do steps 1 and 3 doesn't give us much of a performance gain, because as I said sub + add doesn't bother the CPU.
Step 2 is also missing because the variable is initialized only one time.
BUT, in this case this is what we are doing:
PHP Code:
static falling_num
falling_num = pev(id, pev_flFallVelocity)
We write to the memory location of the variable every time, so that's no better than having it initialized every time. Sure, if it was new then we would have 2 writes: when it is set to 0 and then when we write to it manually(falling_num = pev(...)), but again the variable is only 4 bytes so it's not a problem for the CPU: set 4 bytes once vs set them twice.
Things start to change when we deal with arrays.
When declared with new, steps 1 and 3 are the same, subtract and add. However, for step 2 we need to set a bigger memory block to 0: 4 bytes * array size. For a big array, this can make a difference.
When it is static we don't need to set it to 0 every time the function is called, which is a small gain.
tl;dr
The overhead for "new" doesn't come from "allocating"/"deallocating" the memory, it comes from initializing it to 0. For a single variable it doesn't matter, but for a big array, we will have to initialize a bigger memory block every time the function is called.
Making it static eliminates the need of always 0ing the array since static is initialized only one time.
The "optimization" effect of static is just a side effect of what static is supposed to do. In other words, do not look at "static" primarily from a performance point of view.
It is meant to give you a local variable that maintains its value between function calls, like a global, but with scope restricted to that particular function.
PHP Code:
per frame function:
array[4096] //should be static, need to always set a block of memory of size 4 * 4096 to 0
variable //can be static, but will not create a big difference, need to 0 only 4 bytes
regular function called once in a blue moon:
array[4096] //can be new, will be 0ed once in a blue moon, doesn't matter
variable //should be new, no point in static. 0ing 4 bytes once in a blue moon
per frame function:
static variable
variable = 3 //ok we no longer 0 it every time but we write 3 to it every time, we saved one memory write, not a huge deal
__________________