I am trying to decide between several possible practices. Say, my function has a number of if() blocks, that work on data, that is unique to them.
Should I declare and initialize the local (for the block) data inside the block? Does this have runtime performance cost (due to runtime allocation in the stack)?
Or should I declare and/or initialize all variables at function entry, so that is is done in one, possibly faster, operation block?
Or should I seperate the if() blocks in different functions, even though they are only a couple of lines long and used only one in the program?
Or am I ovelooking another, cleaner, option? Is the question even answerable in it's current, general form?
It's a good practice to keep the scope of the variable as small as possible.
If you declare all the variable one time at the beginning, and you don't use
them often in your program. It's no use, it takes more memory.
Also, another advantages of keeping the scope small is that you can reuse
the same names again. (you don't have to invent new names each time you
do something trivial).
Now the answer concerning performance.
Allocation of local variables is practically free. In most cases, it will really be free, because the update of the stack pointer is performed in the same instruction that writes the value to the stack. Deallocation is either free as well (when something is popped off the stack), or done once at the return (when a stack frame had been created).
While allocation is virtually free, running constructors/destructors is not. While this does not apply to variables of primitive types, it applies to virtually all user defined types, including smart pointers and the like. If you declare a smart pointer at the beginning of the function, but only use it half of the time, you construct, and subsequently destruct the smart pointer twice as much as needed.
Also, if you declare a variable where you have the information to initialize it to your needs, you can construct it directly to the state you want it to have instead of first default constructing it only to change it's value afterwards (using the assignment operator in many cases). So, from a performance perspective, you should always declare variables late and only in the blocks that need them.
No, this is completely contraproductive from a performance perspective. Each function call has an overhead, I think it's between 10 and 20 cycles most of the time. You can do quite a bit of calculation in that time.
For completeness; another, usually less important consideration is stack padding control / packing, which is intuitively more difficult if you don't declare everything upfront.
See this for more information, although let me emphasize the following paragraph before anyone does anything crazy:
Out of the options you are stating, declare and initialize the local (for the block) data inside the block is what will serve your purpose. Forget the rest of the things.
Absolutely: this tends to make programs more readable.
No: all allocations are done upfront - the space on the stack is reserved for variables in all branches upon entering a function, not when the branch is entered. Moreover, this could even save you some space, because the space allocated for variables in non-overlapping branches can be reused by the compiler.
No, this is not faster, and could be slightly more wasteful.
That would probably have a negative impact on readability of your program.