Now the answer is about performance.
Should I declare and initialize local (for the block) data inside the block? Does it have runtime overhead (due to the allocation of runtime on the stack)?
The allocation of local variables is almost free. In most cases, this will be truly free, because updating the stack pointer is done in the same instruction that writes the value to the stack. Freeing is also free (when something pops out of the stack) or is executed once upon return (when the stack frame was created).
Or should I declare and / or initialize all the variables when writing the function, so that this is done in one, possibly faster operating unit?
While distribution is virtually free, running constructors / destructors do not. Although this does not apply to primitive type variables, it applies to almost all user types, including smart pointers and the like. If you declare a smart pointer at the beginning of a function, but use it only half the time, you create and subsequently destroy the smart pointer twice as much as you need.
In addition, if you declare a variable in which you have information to initialize it according to your needs, you can build it directly in the state you want, instead of building it by default only to change its value (using assignment operator in many cases). Thus, in terms of performance, you should always declare variables at the end and only in the blocks they need.
Or should I separate the if () blocks in different functions, although they are only a couple of lines and only one is used in the program?
No, this is completely counterproductive in terms of performance. Each function call has an overhead, I think 10 to 20 cycles most of the time. During this time, you can make several calculations.
cmaster
source share