473,473 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

declaration affects efficiency?

Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.

Aug 8 '06 #1
6 1388

Ivan Liu wrote:
Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.
Typically, it wouldn't matter. It is good practice, though, to keep
the scope of a variable as small as possible.

Best regards,

Tom

Aug 8 '06 #2
"Ivan Liu" <da*******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
: Hi, I wonder if it's more efficient to put variable/object delaration
: outside the for/do-while loop or it doesn't matter.

In many cases, it can actually be more expensive / less efficient
to put the variable outside loop.
So you should not worry about it and do what's best for code
maintenance and readability (i.e. in almost every case put it
in the innermost possible scope), and only look for
optimizations when the need and time has come.
hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Aug 8 '06 #3
Ivan Liu posted:
Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.

If you have something like the following:

for(unsigned i = 0; i != 10; ++i)
{
MyClass obj;
}

, then the constructor is called 10 times, and the destructor is called 10
times.

If you have the following:

MyClass obj;

for(unsigned i = 0; i != 10; ++i)
{

}

, then the constructor is called once, and the destructor is called once.

I'd be quick to say that the latter would be more efficient (i.e. faster),
but then again it all depends on the classes your using, and what kind of
code the compiler makes.

--

Frederick Gotham
Aug 8 '06 #4
Frederick Gotham wrote:
Ivan Liu posted:
Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.


If you have something like the following:

for(unsigned i = 0; i != 10; ++i)
{
MyClass obj;
}

, then the constructor is called 10 times, and the destructor is called 10
times.

If you have the following:

MyClass obj;

for(unsigned i = 0; i != 10; ++i)
{

}

, then the constructor is called once, and the destructor is called once.

I'd be quick to say that the latter would be more efficient (i.e. faster),
but then again it all depends on the classes your using, and what kind of
code the compiler makes.
Precisely: it does depend on the code in question. A relevant quote
from _Efficient C++ Programming_ by Lippman
(http://www.awprofessional.com/articl...p=25033&rl=1):

Matrix mat;
while ( something.more() )
{
mat = something.fetch_mat();
// do something with mat ...
}

mat is set to a different Matrix class object with each iteration. The
programmer could have written

while ( something.more() )
{
Matrix mat = something.fetch_mat();
// do something with mat ...
}

but wished to avoid the construction and destruction of mat with each
loop iteration. Let's rather initialize and destroy it once prior to
and after completion of the loop, the programmer explains, not
realizing that with each iteration, the assignment requires that a
temporary be passed to fetch_mat(), where it is constructed. The
temporary is then copy assigned to mat and destructed. While placing
the definition of mat outside the loop appears more efficient, it is
actually more expensive, resulting in an additional copy assignment
operation with each loop iteration, in addition to the construction and
destruction of a temporary Matrix object.
---

Cheers! --M

Aug 8 '06 #5
So, which one is usually more costly? Temporary copy or unessecary
constructor/destructor?

Aug 8 '06 #6
Ivan Liu wrote:
So, which one is usually more costly? [..]
There is no such thing. You need to *measure* to tell.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #7

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

Similar topics

11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
4
by: Helmut Dirtinger | last post by:
Hi Compontents of an xml file are mapped to the different node types of the xpath data model. An element is mapped to an element node, an attribute node represents an attribute and its value...
3
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
8
by: Sim Smith | last post by:
This is the problem: I have to inherit a third party class file in my library. If I directly do it, I will have to ship the third party header files to my customers as well. I do not want to...
12
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) {...
3
by: JimM | last post by:
I'm curious if anyone knows what the most appropriate way of coding the following situation: (Version 1) Dim x As Integer For i As Integer = 0 To iLimit For j As Integer = 0 To jLimit x =...
39
by: Gaijinco | last post by:
I have always felt that you should only declared variables as needed, and implicitily it seems many authors to encourage it, but the other day a friend told me that declaring variables inside a...
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
2
by: jjw | last post by:
I'm just starting into the process of writing a translator for an XML document. I am confused about xmlns declarations and their affects. According to my Web search, they should not be parsed, yet...
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.