Effiiency of Variable definitions in loops

T

Thomas Matthews

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
 
A

Agent Mulder

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
 
P

Peter van Merkerk

Thomas Matthews said:
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.
 
J

Jason

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top