473,406 Members | 2,620 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,406 software developers and data experts.

passing class to a function in c++ - performance question

Hi,
I'm writing a code that has to be as efficient as possible both in terms of
memory use and execution speed. I'll have to pass a class instance (which is
an 'intelligent' array) to a function (multiple times). Taking this into
consideration, should I pass it as a pointer or reference? Is there any book
or site that teaches stuff like this (optimizing code for performance)?
thanks for any help
Jul 23 '05 #1
8 1392
How references are implemented are up to the compiler writer, however
they're usually implemented as pointers. You could implement your code
both ways and test the runtime performance, or look at the object code
to see what it's doing. However, the rule of thumb is use references
unless you *have* to use pointers. In this case, there is no
requirement specifically for pointers, so use a reference. But if
you're really concerned with performance to the extent of worrying
about the difference between passing a reference or a pointer, maybe
you should write your application in assembler instead of C++. Didn't
somebody famus say, "premature optimization is evil", or something like
that?

Jul 23 '05 #2
Profil1 wrote:

Hi,
I'm writing a code that has to be as efficient as possible both in terms of
memory use and execution speed. I'll have to pass a class instance (which is
an 'intelligent' array) to a function (multiple times). Taking this into
consideration, should I pass it as a pointer or reference? Is there any book
or site that teaches stuff like this (optimizing code for performance)?
thanks for any help


There is only one way to figure out: Time it.

But usually both methods (pass by pointer, pass per reference)
have equal performance. This is so, because the compiler implements
'pass per reference' by passing a pointer under the hood. So in
the end it turns out, that you do the very same thing with different
syntax.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Profil1 wrote:
Hi,
I'm writing a code that has to be as efficient as possible both in terms of
memory use and execution speed. I'll have to pass a class instance (which is
an 'intelligent' array) to a function (multiple times). Taking this into
consideration, should I pass it as a pointer or reference? Is there any book
or site that teaches stuff like this (optimizing code for performance)?
thanks for any help


In practice, usually, passing by pointer vs by reference has the same
performance. However, since a reference cannot be changed once it is
seated, in theory, the optimizer may be more agressive with code
optimizations with references vs optimizations with pointers. So,
passing by reference may yield better optimized code (in theory).

The only real way to find out is to try it.

This also applies to the "register" keyword. The register keyword was
usually a hint to the compiler to allocate a register. An optimizer can
now read that it can't be aliased and hence it can perform better
optimizations. Again, this is theory only, what your compiler does is
the only thing that matters.

Jul 23 '05 #4


Profil1 wrote:
Hi,
I'm writing a code that has to be as efficient as possible both in terms of
memory use and execution speed. I'll have to pass a class instance (which is
an 'intelligent' array) to a function (multiple times). Taking this into
consideration, should I pass it as a pointer or reference? Is there any book
or site that teaches stuff like this (optimizing code for performance)?
thanks for any help


If you want to improve performance first profile your code to determine
where the time is spent. If as a result the function which is being
called is really shown to be the bottleneck contrive to call the
function fewer times. I.e., find an algorithm that is O(log n) rather
than O(n^2). Then look to improving the way that the function does its
work for example remove sqrt(), etc.
Jul 23 '05 #5
lilburne wrote:


Profil1 wrote:
Hi,
I'm writing a code that has to be as efficient as possible both in
terms of memory use and execution speed. I'll have to pass a class
instance (which is an 'intelligent' array) to a function (multiple
times). Taking this into consideration, should I pass it as a pointer
or reference? Is there any book or site that teaches stuff like this
(optimizing code for performance)?
thanks for any help


If you want to improve performance first profile your code to determine
where the time is spent. If as a result the function which is being
called is really shown to be the bottleneck contrive to call the
function fewer times. I.e., find an algorithm that is O(log n) rather
than O(n^2). Then look to improving the way that the function does its
work for example remove sqrt(), etc.


Seconded.

Trying to optimize code at this level will be an exercise in wasting
time. Premature Optimisation as BigBrian said.

The real bottle neck will be elsewhere, not whether a ref or pointer is
passed.

Jul 23 '05 #6
Andrew McDonagh wrote:
lilburne wrote:

If you want to improve performance first profile your code to
determine where the time is spent. If as a result the function which
is being called is really shown to be the bottleneck contrive to call
the function fewer times. I.e., find an algorithm that is O(log n)
rather than O(n^2). Then look to improving the way that the function
does its work for example remove sqrt(), etc.


Seconded.

Trying to optimize code at this level will be an exercise in wasting
time. Premature Optimisation as BigBrian said.

The real bottle neck will be elsewhere, not whether a ref or pointer is
passed.


Sometimes micro optimization can be successful, but you need
a tool, like quantify, to point it out. Guessing and
supposing is nearly always wrong.
Jul 23 '05 #7
lilburne wrote:
Andrew McDonagh wrote:
lilburne wrote:

If you want to improve performance first profile your code to
determine where the time is spent. If as a result the function which
is being called is really shown to be the bottleneck contrive to call
the function fewer times. I.e., find an algorithm that is O(log n)
rather than O(n^2). Then look to improving the way that the function
does its work for example remove sqrt(), etc.


Seconded.

Trying to optimize code at this level will be an exercise in wasting
time. Premature Optimisation as BigBrian said.

The real bottle neck will be elsewhere, not whether a ref or pointer
is passed.


Sometimes micro optimization can be successful, but you need a tool,
like quantify, to point it out. Guessing and supposing is nearly always
wrong.


agreed, like you say, so long as we have proof from some a tool like
Rational's Quantify.

The problems start when a developer 'knows' that doing it 'this way' is
faster than that way, even if it means the code is less clear and
usually tightly coupled, when in fact the area of concern is called once
in a blue moon.

Jul 23 '05 #8
If you're going to do any performance testing, don't forget to do them
on a Release build. :) Optimizing debug code is not recommended as it
may defeat the compiler's optimizer, not to mention that the end
(Release) code will probably be different from what you expected,
anyway.

Also, before you try to fix it, find out exactly where it's broken. Use
a profiler to identify hotspots. You'll get much better results this
way.

Hope this helps,

Aleko
Profil1 wrote:
Hi,
I'm writing a code that has to be as efficient as possible both in terms of memory use and execution speed. I'll have to pass a class instance (which is an 'intelligent' array) to a function (multiple times). Taking this into consideration, should I pass it as a pointer or reference? Is there any book or site that teaches stuff like this (optimizing code for performance)? thanks for any help


Jul 23 '05 #9

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

Similar topics

25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
2
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
3
by: Simon Harvey | last post by:
Hi, In my application I get lots of different sorts of information from databases. As such, a lot of information is stored in DataSets and DataTable objects. Up until now, I have been passing...
8
by: the_real_remi | last post by:
Hi, I'm writing a code that has to be as efficient as possible both in terms of memory use and execution speed. I'll have to pass a class instance (which is an 'intelligent' array) to a function...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
5
by: Markus Ernst | last post by:
Hello A class that composes the output of shop-related data gets some info from the main shop class. Now I wonder whether it is faster to store the info in the output class or get it from the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
4
by: Philip Semanchuk | last post by:
I'm writing a Python extension in C that wraps a function which takes a void * as a parameter. (The function is shmat() which attaches a chunk of shared memory to the process at the address...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...

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.