473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Will frequent local definitions hurt performance.

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 '07 #1
3 1269
lovecreatesbea...@gmail.com 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?
Jan 7 '07 #2

Ron Natalie skrev:
lovecreatesbea...@gmail.com 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

Jan 7 '07 #3
lovecreatesbea...@gmail.com 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.

Jan 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: test1dellboy3 | last post by:
Hi, I am a beginner using the python interpreter. To reduce typing effort, I created a module called "aliases.py" containing some aliases for objects I commonly use like - aliases.py : ...
2
by: Marc | last post by:
I am trying to create a new window from which the original page can close that new window. For some reason the following code will not work. Passing the window.open command to a variable called...
10
by: B Moor | last post by:
Host PC: Win SBS 2003 with 2.6 GHz processor and 1GB RAM SQL Server 2000 sp3 (or latest).MS Office 2k3 I have the access 2k3 front end running on server (this may get changed) and all was well...
2
by: Doc | last post by:
Per earlier post, I am trying to save 'out' production data from a program called Solomon - basically (I think) this was /is an Access/Sql based program. We are updating to different application...
1
by: Julia | last post by:
Hi, I have basic questions regarding ASP.NET site I am going to use Microsoft application configuration block and I wonder 1.does caching read only data in the application can hurt ...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
4
by: Ryan Liu | last post by:
TcpClient has a method called GetworkStream GetStream(); So in other words, there is only one stream associate with it for input and output, right? So while it is receiving, it can not send, and...
11
by: Paul H | last post by:
Suppose I have a table called tblPeople and I want a field to illustrate whether each person prefers cats or dogs. I could do it one of three ways. 1. A plain text field Create a text field in...
23
by: deepakvsoni | last post by:
Why does C++ restrict definition of functions with in functions? int f1() { int f2() { ..... } } //This is not supported. Thanks in advance to anybody who replies..
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.