Effiiency of Variable definitions in loops

Discussion in 'C++' started by Thomas Matthews, Aug 21, 2003.

  1. 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
     
    Thomas Matthews, Aug 21, 2003
    #1
    1. Advertising

  2. Thomas Matthews

    Agent Mulder Guest

    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
     
    Agent Mulder, Aug 21, 2003
    #2
    1. Advertising

  3. "Thomas Matthews" <> wrote in
    message news:4d61b.28625$...
    > 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
     
    Peter van Merkerk, Aug 21, 2003
    #3
  4. Thomas Matthews

    Jason Guest

    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"
     
    Jason, Aug 21, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrice
    Replies:
    1
    Views:
    1,665
    =?Utf-8?B?REZyYW5rbHlu?=
    May 13, 2004
  2. Roedy Green
    Replies:
    4
    Views:
    454
  3. Charles L
    Replies:
    5
    Views:
    1,217
    Ron Natalie
    Feb 8, 2005
  4. Bob Sanders

    Ruby loops and definitions help?

    Bob Sanders, May 15, 2009, in forum: Ruby
    Replies:
    1
    Views:
    99
    Pascal J. Bourguignon
    May 15, 2009
  5. Me
    Replies:
    2
    Views:
    247
Loading...

Share This Page