markzzz
markzzz

Reputation: 47945

Declare var outside loop is bad?

I wrote this basic code for a DSP/audio application I'm making:

double input = 0.0;
for (int i = 0; i < nChannels; i++) {
      input = inputs[i];

and some DSP engineering expert tell to me: "you should not declare it outside the loop, otherwise it create a dependency and the compiler can't deal with it as efficiently as possible."

He's talking about var input I think. Why this? Isn't better decleare once and overwrite it?

Maybe somethings to do with different memory location used? i.e. register instead of stack?

Upvotes: 1

Views: 191

Answers (3)

user555045
user555045

Reputation: 64904

Many people think that declaring a variable allocates some memory for you to use. It does not work like that. It does not allocate a register either.

It only creates for you a name (and an associated type) that you can use to link consumers of values with their producers.

On a 50 year old compiler (or one written by students in their 3rd year Compiler Construction course), that may be implemented by indeed allocating some memory for the variable on the stack, and using that every time the variable is referenced. It's simple, it works, and it's horribly inefficient. A good step up is putting local variables in registers when possible, but that uses registers inefficiently and it's not where we're at currently (have been for some time).

Linking consumers with producers creates a data flow graph. In most modern compilers, it's the edges in that graph that receive registers. This is completely removed from any variables as you declared them. They no longer exist. You can see this in action if you use -emit-llvm in clang.

So variables aren't real, they're just labels. Use them as you want.

Upvotes: 3

Serge Ballesta
Serge Ballesta

Reputation: 148880

Good old K&R C compilers in the early eighties used to produce code as near as possible what the programmer wrote, and programmers used to do their best to produce optimized source code. Modern optimizing compilers can rework things provided the resulting code has same observable effects as the original code. So here, assuming the input variable is not used outside the loop, an optimizing compiler could optimize out the line double input = 0.0; because there are no observable effects until next assignation : input = inputs[i];. And it could the same factor the variable assignation outside the loop (whether in source C++ file it is inside or not) for the same reason.

Short story, unless you want to produce code for one specific compiler with one specific parameters set, and in that case you should thoroughly examine the generated assembly code, you should never worry for those low level optimizations. Some people say compiler is smarter than you, other say compiler will produce its own code whatever way I wrote mine.

What matters is just readability and variable scoping. Here input is functionaly local to the loop, so it should be declared inside the loop. Full stop. Any other optimization consideration is just useless, unless you do have special requirements for low level optimization (profiling showing that these lines require special processing).

Upvotes: 6

user2512323
user2512323

Reputation:

It is better to declare variable inside the loop, but the reason is wrong.

There is a rule of thumb: declare variables in the smallest scope possible. Your code is more readable and less error prone this way.

As for performance question, it doesn't matter at all for any modern compiler where exactly you declare your variables. For example, clang eliminates variable entirely at -O1 from its own IR: https://godbolt.org/g/yjs4dA

One corner case, however: if you ever takes an address of input, variable can't be eliminated (easily), and you should declare it inside the loop, if you care about performance.

Upvotes: 3

Related Questions