By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,189 Members | 933 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,189 IT Pros & Developers. It's quick & easy.

Effiiency of Variable definitions in loops

P: n/a
Hi,

While coding programs, I cam about a conundrum regarding
variables defined in an iterative loop.
The issue is whether it is more efficient to factor the
definition out of the loop or maintain encapsulation by
leaving it inside the loop?

Common stuff for examples:
class Data;
bool Is_Data_Valid(const Data &);
ifstream data_file;

Example 1: Definition inside loop:
Data my_datum;
while (data_file >> my_datum)
{
bool status; // *** Definition within loop. ***
status = Is_Data_Valid(my_datum);
if (!status)
return;
}

Example 2: Definition outside loop:
Data my_datum;
bool status; // *** Definition outside of loop ***
while (data_file >> my_datum)
{
status = Is_Data_Valid(my_datum);
if (!status)
return;
}
Reasonings:
1. In the first example, the variable is defined where
it is used. The variable is only used within the
scope of the loop.
2. The variable in the first example is created for
each iteration of the loop.
3. In the second example, the variable is available
outside of the loop (perhaps poor encapsulation).
4. However, the variable is only created once.

Any recommendations, or is this a religious issue?

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
TM> The issue is whether it is more efficient to factor the
TM> definition out of the loop or maintain encapsulation by
TM> leaving it inside the loop?

With POD and simple objects I normally
create them in the loop. It allows syntax
like this

bool status=Is_Data_Valid(my_datum);

or even

if(!Is_Data_Valid(my_datum))return;

removing the need for the bool variable alltogether.
More complex objects that are more expensive to
create are created before the start of the loop, if not
as class members.

-X
Jul 19 '05 #2

P: n/a
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in
message news:4d**********************@newssvr28.news.prodi gy.com...
Hi,

While coding programs, I cam about a conundrum regarding
variables defined in an iterative loop.
The issue is whether it is more efficient to factor the
definition out of the loop or maintain encapsulation by
leaving it inside the loop?

Common stuff for examples:
class Data;
bool Is_Data_Valid(const Data &);
ifstream data_file;

Example 1: Definition inside loop:
Data my_datum;
while (data_file >> my_datum)
{
bool status; // *** Definition within loop. ***
status = Is_Data_Valid(my_datum);
if (!status)
return;
}

Example 2: Definition outside loop:
Data my_datum;
bool status; // *** Definition outside of loop ***
while (data_file >> my_datum)
{
status = Is_Data_Valid(my_datum);
if (!status)
return;
}

Reasonings:
1. In the first example, the variable is defined where
it is used. The variable is only used within the
scope of the loop.
2. The variable in the first example is created for
each iteration of the loop.
3. In the second example, the variable is available
outside of the loop (perhaps poor encapsulation).
4. However, the variable is only created once.

Any recommendations, or is this a religious issue?


Primitive types like bool do not have construction/destruction overhead
(i.e.. the declaration itself costs nothing), so there is no performance
penalty by limiting its scope to inside the loop. It might even help the
optimizer if you make it explicit that the variable is only needed
inside the loop and its value does not need to be propagated to the next
iteration. But most likely it will not make a difference since a good
optimizers can come to that conclusion also if the variable were defined
outside the loop.

For classes with non-trivial constructors/destructors (e.g. std::string)
the story is different. They need to be constructed and destructed every
iteration of the loop, so there is a performance penalty.

My recommendation is to limit the variable to the smallest scope
possible, unless there is performance problem caused by the
construction/destruction overhead of that variable. Another
recommendation I have is to postpone variable definitions as long as
possible, so they can be immediately initialized. Instead of:

bool status;
...
status = Is_Data_Valid(my_datum);

use:
...
bool status = Is_Data_Valid(my_datum);

This style prevents accidental use of uninitialized variables and can be
more efficient with classes with non-trivial default constructors.
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl

Jul 19 '05 #3

P: n/a
It's likely, in my opinion, not to affect it substantially in most cases, if
at all, since a compiler may well generate code to allocate space for all
local variables at the same time on entry to a function; no matter their
scope. It's only in the code that there is this thing we call "lexical
scope"
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.