c++ - Declare var outside loop is bad? -
i wrote basic code dsp/audio application i'm making:
double input = 0.0; (int = 0; < nchannels; i++) { input = inputs[i];
and dsp engineering expert tell me: "you should not declare outside loop, otherwise create dependency , compiler can't deal efficiently possible."
he's talking var input
think. why this? isn't better decleare once , overwrite it?
maybe somethings different memory location used? i.e. register instead of stack?
many people think declaring variable allocates memory use. not work that. not allocate register either.
it creates name (and associated type) can use link consumers of values producers.
on 50 year old compiler (or 1 written students in 3rd year compiler construction course), may implemented indeed allocating memory variable on stack, , using every time variable referenced. it's simple, works, , it's horribly inefficient. step putting local variables in registers when possible, uses registers inefficiently , it's not we're @ (have been time).
linking consumers producers creates data flow graph. in modern compilers, it's edges in graph receive registers. removed variables declared them. no longer exist. can see in action if use -emit-llvm in clang.
so variables aren't real, they're labels. use them want.
Comments
Post a Comment