Will frequent local definitions hurt performance.

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

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 */
}
 
R

Ron Natalie

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?
 
P

peter koch

Ron Natalie skrev:
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
 
G

Grizlyk

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.
 

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

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top