How is conditional initialization handled and is it good practice?

I am trying to solve several possible practices. Let's say my function has several if () blocks that work with data that is unique to them.

  • 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)?

  • 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?

  • 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?

Or am I exploring another, cleaner option? The question is, does even the current general form answer in it?

+8
performance c coding-style
source share
5 answers

Should I declare and initialize local (for the block) data inside the block?

Absolutely: this makes programs more readable.

Does this have a runtime cost (due to the allocation of runtime on the stack)?

No: all distributions are performed in advance - the space on the stack is reserved for variables in all branches when entering a function, and not when entering a branch. Moreover, it can even save you some space, because the space allocated for variables in nonoverlapping branches can be reused by the compiler.

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?

No, it's not faster and can be a little more wasteful.

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?

This is likely to negatively affect the readability of your program.

+8
source share

It’s good practice to keep the scope of the variable as small as possible.
If you declare the whole variable once at the beginning and do not use them often in your program. It is useless, requires more memory.

In addition, another advantage of maintaining a small volume is that you can reuse the same names again. (you don't need to come up with new names every time you
do something trivial).

+4
source share

From the parameters that you specify, declare and initialize local (for the block) data inside the block, which will serve your purpose. Forget about other things.

0
source share

For completeness; another, usually less important consideration is inventory management / packaging, which is intuitively more complex if you do not announce everything in advance.

See this for more information, although let me emphasize the following paragraph before anyone does anything crazy:

Usually for a small number of scalar variables in your C programs, selecting a few bytes that you can get by changing the order of the declaration will not save you to be significant. The technique becomes more interesting when applied to non-scalar variables - especially structures.

0
source share

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.

0
source share

All Articles