Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Will frequent local definitions hurt performance.

Reply
Thread Tools

Will frequent local definitions hurt performance.

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      01-07-2007
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 */
}

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-07-2007
(E-Mail Removed) 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?
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      01-07-2007

Ron Natalie skrev:
> (E-Mail Removed) 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

 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007
(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Switched to LCD monitor from CRT - my eyes hurt Weapons of Mass Destruction Computer Support 6 10-01-2010 04:46 PM
Seeking song ID "is everybody hurt" Brian Computer Support 4 09-24-2005 09:05 PM
can Cached read only data hurt performance? Julia ASP .Net 1 02-28-2005 10:19 AM
Do shtml hurt search engine crawlings? Q&A HTML 4 09-01-2004 11:06 PM
why does it hurt when i pee? { Listerine Enema } Computer Support 5 06-04-2004 01:37 AM



Advertisments