G
Guest
Using local declarations within a block often makes code more readable, but
is it less efficient? eg...
void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}
At first sight this looks inefficient, because i and b are being allocated
each time around the loop. But I wonder if that is really the case. I have
heard that when procedure P is placed on the stack, that *all* variables
within P, including those within a nested scope, are also allocated. ie. in
the above example, i and b would be allocated only once, at the start of the
procedure. However, I am not sure whether this is the true.
What if the the variable is nested within a conditional - is it still always
allocated at the start? eg...
while (...) {
if (...) {
int i = ...;
bool b = ...;
....
}
}
Any experts here who can ease my mind about the possible ineffiency of
nested variables?
A bit of background - I'm writing a real time application, with several
complex loops processing data which is being received at a high rate, so I
keep coming across this dilemna.
TIA,
Javaman
is it less efficient? eg...
void P() {
while (...) {
int i = ...;
bool b = ...;
....
}
}
At first sight this looks inefficient, because i and b are being allocated
each time around the loop. But I wonder if that is really the case. I have
heard that when procedure P is placed on the stack, that *all* variables
within P, including those within a nested scope, are also allocated. ie. in
the above example, i and b would be allocated only once, at the start of the
procedure. However, I am not sure whether this is the true.
What if the the variable is nested within a conditional - is it still always
allocated at the start? eg...
while (...) {
if (...) {
int i = ...;
bool b = ...;
....
}
}
Any experts here who can ease my mind about the possible ineffiency of
nested variables?
A bit of background - I'm writing a real time application, with several
complex loops processing data which is being received at a high rate, so I
keep coming across this dilemna.
TIA,
Javaman