473,605 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple code performance question

Hi,

Given this code:

A** ppA = new A*[10];
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA[i];
//do something with pA
}

- is there some performance penalty if pA declaration and assignment
will be inside the for-block as:

A *pA = ppA[i];

Regards,
ren

Oct 30 '07 #1
30 3501
ga********@gmai l.com wrote:
Given this code:

A** ppA = new A*[10];
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA[i];
//do something with pA
}

- is there some performance penalty if pA declaration and assignment
will be inside the for-block as:

A *pA = ppA[i];
Of cource not. However, the declaration/definition/initialisation
inside "the for-block" is much better from the code maintenance POV.
Presence of 'pA' variable outside has *no merit*. It only pollutes
the scope. Unless there is a need to use 'pA' in the same scope as
'ppA', 'pA' should be defined inside.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '07 #2
Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?
TIA
Oren

Oct 31 '07 #3
gl****@smile.ne t.il wrote:
Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?
Yes. What details are you looking for?

Let's say this: with a very bad compiler (which doesn't emit the
same code in both cases) there might be some difference, but so
miniscule that it would not matter when the big[ger] picture is
concerned.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '07 #4
What is the difference between these cases in a bad compiler?
Is it that the second case allocates the pointer's memory on each loop
and this causes the (negligible) performance degradation?

Oct 31 '07 #5
gl****@smile.ne t.il wrote:
What is the difference between these cases in a bad compiler?
Is it that the second case allocates the pointer's memory on each loop
and this causes the (negligible) performance degradation?
Something like that. You should not expect such allocation to take too
much time, of course, even with a bad compiler. Automatic storage is
usually not overly slow. That's why the degradation (if any) is truly
negligible.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '07 #6
On Oct 31, 9:36 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
glo...@smile.ne t.il wrote:
Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?
Yes. What details are you looking for?
Let's say this: with a very bad compiler (which doesn't emit the
same code in both cases) there might be some difference, but so
miniscule that it would not matter when the big[ger] picture is
concerned.
More importantly, the difference can go both ways.

A while back, someone suggested declaring an std::string outside
the loop, on the grounds that that would improve performance,
since the constructor and the destructor would only have to be
invoked once, and not each time through the loop. I wrote up a
small benchmark to see just how much difference it would make,
and with g++ (2.95.2 at the time, I think), it turned out that
declaring the variable in the loop was actually significantly
faster. For my benchmark---it all depended on what you were
doing.

The correct answer is (and I know you know it): don't worry
about it until the profiler says you have to, and then try both,
measuring, to see what actually happens on your implementation.
Anything else is just pure stupidity.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 1 '07 #7
On 1 nov, 06:36, James Kanze <james.ka...@gm ail.comwrote:
On Oct 31, 9:36 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
glo...@smile.ne t.il wrote:
Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?
Yes. What details are you looking for?
Let's say this: with a very bad compiler (which doesn't emit the
same code in both cases) there might be some difference, but so
miniscule that it would not matter when the big[ger] picture is
concerned.

More importantly, the difference can go both ways.

A while back, someone suggested declaring an std::string outside
the loop, on the grounds that that would improve performance,
since the constructor and the destructor would only have to be
invoked once, and not each time through the loop. I wrote up a
small benchmark to see just how much difference it would make,
and with g++ (2.95.2 at the time, I think), it turned out that
declaring the variable in the loop was actually significantly
faster. For my benchmark---it all depended on what you were
doing.
How could that be possible? With a minimum of smartness in memory
management, both ways should be equally efficient in this regard. The
execution of any kind of code in constructors/destructors implicit
calls could also be easily avoided for std::strings when gcc detects
such constructs (though such optimization may not always be possible
for user-defined classes). But this only means that gcc should have
the same performance in both cases, otherwise it must not be doing a
good job on optimizing the constructor-outside-the-loop case.

In my humble opinion, it is safier to believe that code like this will
run faster:

#include <string>

void get_string_some how( std::string& str ); //Elsewhere implemented.
void do_something_wi th_string( std::string& str ); //Elsewhere
implemented.

int main();
{
{
std::string str;
for ( unsigned i( 0 ); i < 100; ++i )
{
get_string_some how( str );
do_something_wi th_string( str );
}
}

return( 0 );
}

Notice that concerns about name leaking are adressed by simply placing
braces {} around the relevant code. When it comes to pointers and
other fundamental types, however, I pretty much agree that declaring
inside loops is the way to go.
>
The correct answer is (and I know you know it): don't worry
about it until the profiler says you have to, and then try both,
measuring, to see what actually happens on your implementation.
Anything else is just pure stupidity.
I would say that some precaution when coding, i.e. prior to proper
profiling be possible, cannot be considered stupidity, but I do agree
that no a-priori assertion can be done in this repect.

Elias Salomão Helou Neto

Nov 2 '07 #8
On Nov 2, 1:45 am, Elias Salomão Helou Neto <eshn...@gmail. comwrote:
On 1 nov, 06:36, James Kanze <james.ka...@gm ail.comwrote:
On Oct 31, 9:36 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
glo...@smile.ne t.il wrote:
Thanks for the answer, but can you supply some more details?
Why there is no performance penalty here, is it because it is a
pointer type?
Yes. What details are you looking for?
Let's say this: with a very bad compiler (which doesn't emit the
same code in both cases) there might be some difference, but so
miniscule that it would not matter when the big[ger] picture is
concerned.
More importantly, the difference can go both ways.
A while back, someone suggested declaring an std::string outside
the loop, on the grounds that that would improve performance,
since the constructor and the destructor would only have to be
invoked once, and not each time through the loop. I wrote up a
small benchmark to see just how much difference it would make,
and with g++ (2.95.2 at the time, I think), it turned out that
declaring the variable in the loop was actually significantly
faster. For my benchmark---it all depended on what you were
doing.
How could that be possible?
Who knows? Who cares? I didn't do an extensive analysis of the
implementation of g++. The point remains that you cannot say
which is faster (if either) until you've measured the case which
interests you.
With a minimum of smartness in memory management, both ways
should be equally efficient in this regard. The execution of
any kind of code in constructors/destructors implicit calls
could also be easily avoided for std::strings when gcc detects
such constructs (though such optimization may not always be
possible for user-defined classes). But this only means that
gcc should have the same performance in both cases, otherwise
it must not be doing a good job on optimizing the
constructor-outside-the-loop case.
Or the implementation of std::string does something funny. Or
whatever. All it means is that assignment is more expensive
than construction/destruction, which shouldn't really supprise
anyone.
In my humble opinion, it is safier to believe that code like
this will run faster:
#include <string>
void get_string_some how( std::string& str ); //Elsewhere implemented.
void do_something_wi th_string( std::string& str ); //Elsewhere
implemented.
int main();
{
{
std::string str;
for ( unsigned i( 0 ); i < 100; ++i )
{
get_string_some how( str );
do_something_wi th_string( str );
}
}
return( 0 );
}
I don't know what you mean by "safer", but there's absolutely no
reason to believe anything of the kind. Depending on the
implementation of std::string, what get_string_some how does, and
possibly any number of other factors, it's impossible to say
without measuring whether the above will be faster or slower
than any other particular solution.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 2 '07 #9
Thanks for all posters.
Oren

Nov 2 '07 #10

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

Similar topics

5
2217
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its nitty gritty but its necessary for my program. thanks dave
2
8382
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
3
1476
by: Andrew Jocelyn | last post by:
Hi I have a simple ASP.NET web page with a single label control: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="default.aspx.vb" Inherits="proj.TheTime" EnableViewState="false" EnableSessionState="false"%> .... <p> <asp:label id="Label1" runat="server">Label</asp:label></p> ....
10
2634
by: GeekBoy | last post by:
Okay, I have two identical web servers running Windows 2003 web server. I have an ASP.NET application which runs great on one of them. Dedicated IP address, behind our firewall, etc. Everyone's happy. Now -- how do I take advantage of that second computer to "load-balance" the web site? Will it really give my users a noticable performance increase? How do you accomplish this? I've read many of those MS articles and it's...
5
1695
by: Byron | last post by:
I need to create an application that uses primarily a single form rather than an SDI that creates a new form for everythting. However, I don't want an MDI style application since the users I'm dealing with would be overwhelmed with it. The approach I tried in the past was to use MDI, but made all the child windows occupy the entire parent area without a frame so it looked like a single window. I also allowed only a single instance of...
13
2028
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python folks might find to taste. Here's the code - first the 'engine', then some code demonstrating the usage patterns. For me (and maybe for some of you), it promotes readability and some
10
1811
by: Sourcerer | last post by:
I wrote this very simple code in .NET VC++. I compiled it on my system, and tried to run it on my friend's computer (he doesn't have the compiler). We both have Windows XP Professional. I have .NET framework 2.0, and he had 1.0 and it didn't work; then he installed 2.0 and it still didn't work; so he tried with 2.1 and it didn't work, then 3.0 and nothing still worked. I have Intel Centrino Mobile (laptop computer), and he has Intel Pentium...
13
1528
by: aaragon | last post by:
Hi everyone, I just wanted to know if there is any difference in performance in declarating the variables in the beginning of a function or within for loops. For example: double test() { double a,b,c; for(int i=0; i<n; i++){
7
1803
by: CSharper | last post by:
Yesterday I had a heated discussion with my colleagues on what is a data centric application and having business logic in sql. I have group of people who wants to include all the business logic in the sql stored proc and I for one consider to have most of the business logic in the C# code (specifically in a controller class of MVC). These are my points to them. 1. Our application depends a lot on the data and we have huge amount of data...
0
7999
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7931
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8411
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6740
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5885
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3911
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2437
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 we have to send another system
1
1530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1270
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.