Will frequent local definitions hurt performance.

Discussion in 'C++' started by lovecreatesbea...@gmail.com, Jan 7, 2007.

  1. Guest

    Is case 2 better than case 1 in performance? For case 2 doesn't create
    and destroy objects inside a loop again and again for 1000 times.

    /*case 1: local definitions inside a loop*/
    for (int i = 0; i != 1000; ++i){
    int a1;
    int a2;
    /* more local variables */
    int an;

    /* more operations on variables: a1, a2, ..., an */
    }

    /*case 2: definitions outside a loop*/
    int b1;
    int b2;
    /* more local variables */
    int bn;

    for (int i = 0; i != 1000; ++i){
    /* more operations on variables: b1, b2, ..., bn */
    }
     
    , Jan 7, 2007
    #1
    1. Advertising

  2. Ron Natalie Guest

    wrote:
    > Is case 2 better than case 1 in performance? For case 2 doesn't create
    > and destroy objects inside a loop again and again for 1000 times.
    >

    ints don't have any construction or destruction (their default
    initialization is even omitted here). All the compilers I've
    used allocate the memory for code inside a block once (and
    often at the time the surrounding function is called) rather
    than on each invocation of the block. The compiler would
    typically generate the same code for both your examples.

    However, if there is a non-trivial construction involved
    (that is the variables are classes). Then you have to
    ask:

    1. Is it more efficient to do the construction and
    destruction once outside the loop?

    2. Do I need / want the variables reinititialized
    each time or can I tolerate them being left to the
    state from the previous iteration?
     
    Ron Natalie, Jan 7, 2007
    #2
    1. Advertising

  3. peter koch Guest

    Ron Natalie skrev:
    > wrote:
    > > Is case 2 better than case 1 in performance? For case 2 doesn't create
    > > and destroy objects inside a loop again and again for 1000 times.
    > >

    > ints don't have any construction or destruction (their default
    > initialization is even omitted here). All the compilers I've
    > used allocate the memory for code inside a block once (and
    > often at the time the surrounding function is called) rather
    > than on each invocation of the block. The compiler would
    > typically generate the same code for both your examples.
    >
    > However, if there is a non-trivial construction involved
    > (that is the variables are classes). Then you have to
    > ask:
    >
    > 1. Is it more efficient to do the construction and
    > destruction once outside the loop?
    >
    > 2. Do I need / want the variables reinititialized
    > each time or can I tolerate them being left to the
    > state from the previous iteration?


    And:
    3. Does it really matter - is it time-critical here and worth
    "obfuscating" the code by putting the variable in the wrong scope?
    4. Could it be that the proper initialisation shows up to be
    computation optimal also? Often by having the variables outside your
    loop you end up having more con- and destructions (this time of
    temporary variables).

    /Peter
     
    peter koch, Jan 7, 2007
    #3
  4. Grizlyk Guest

    wrote:

    > Is case 2 better than case 1 in performance? For case 2 doesn't create
    > and destroy objects inside a loop again and again for 1000 times.


    If your logic requre reinit vars for each pass, it is better to put
    vars into loop body, if not, you just can not place them into loop body.
     
    Grizlyk, Jan 12, 2007
    #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. Julia
    Replies:
    1
    Views:
    348
    =?Utf-8?B?S29zdGFkaW4gS29zdG92?=
    Feb 28, 2005
  2. Q&A
    Replies:
    4
    Views:
    552
  3. Kirk Strauser

    Pickling and inheritance are making me hurt

    Kirk Strauser, Feb 4, 2005, in forum: Python
    Replies:
    2
    Views:
    299
    Tim Peters
    Feb 6, 2005
  4. Daniel Bickett
    Replies:
    2
    Views:
    312
    Daniel Bickett
    Feb 6, 2005
  5. Brian van den Broek
    Replies:
    4
    Views:
    254
    Michele Simionato
    Feb 9, 2005
Loading...

Share This Page