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

P

Profil1

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
 
B

BigBrian

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?
 
K

Karl Heinz Buchegger

Profil1 said:
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.
 
G

Gianni Mariani

Profil1 said:
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.
 
L

lilburne

Profil1 said:
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.
 
A

Andrew McDonagh

lilburne said:
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.
 
L

lilburne

Andrew said:
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.
 
A

Andrew McDonagh

lilburne said:
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.
 
A

aleko

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
 
A

Andre Caldas

should I pass it as a pointer or reference? Is there any book
or site that teaches stuff like this (optimizing code for performance)?

If you don't accept any NULLs, then pass it as reference.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top