By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.