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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
the_real_remi said:
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

Time wasted to write this letter is far greater than time gained by
implementing the "better" choice (if any). It's even greater then
cumulative time gained for the life of the application (a couple of
years). You should seek elsewhere for performance improvements.

I don't know of any book that could teach you better optimization, but
there's a book that could teach you all sorts of useful bits of
knowledge in various fields of software engineering: "Facts and
Fallacies of Software Engineering" by Robert L. Glass. Very inspiring.
 
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?

I don't think there's any performance difference - chose whichever
seems most appropriate in your circumstance. My rule of thumb is to
only use a pointer if the function can realistically accept a NULL
pointer, otherwise I use a reference (preferably const).

Dave
 
the_real_remi 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's no difference. If all you do is change from a reference to a
pointer (and make the necessary syntax adjustments), it's likely the
compiler will generate identical code.

As for a reference on this stuff, yes, there is:

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1666.pdf

(Near as I can tell that's the latest version of the document - if someone
knows of a later one, please chime in).

-cd
 
Time wasted to write this letter is far greater than time gained by
implementing the "better" choice (if any). It's even greater then
cumulative time gained for the life of the application (a couple of
years). You should seek elsewhere for performance improvements.

I don't know of any book that could teach you better optimization, but
there's a book that could teach you all sorts of useful bits of
knowledge in various fields of software engineering: "Facts and
Fallacies of Software Engineering" by Robert L. Glass. Very inspiring.

I have read this book, and while I don't remember it that well, it was
realistic and valuable.

There are usually very few places in an application that require
optimization; but those that do *really* require it.

Some functions, such as many in signal and image processing, need much
more of both algorithmic and line-by-line optimization, sometimes to
the point of different code for different systems, just for the
function to be usable.

Additionally, memory usage is often more of an optimization issue than
CPU performance.
 
Severian said:
Additionally, memory usage is often more of an optimization issue than
CPU performance.

True, and even more so for external memory (hard disks) and
communication (sockets, pipes, serial ports...).

While we're at it I have a question. Is there a portable,
object-oriented (maintainable and extendable) solution for socket
communication that uses the most efficient approach for a given platform
(IO completion ports with thread pool on Win2000, something else for Linux)?
 
True, and even more so for external memory (hard disks) and
communication (sockets, pipes, serial ports...).

While we're at it I have a question. Is there a portable,
object-oriented (maintainable and extendable) solution for socket
communication that uses the most efficient approach for a given platform
(IO completion ports with thread pool on Win2000, something else for Linux)?

I'm not personally aware of one; I would recommend poking around
sourceforge for socket libraries.
 
Mihajlo said:
True, and even more so for external memory (hard disks) and
communication (sockets, pipes, serial ports...).

While we're at it I have a question. Is there a portable,
object-oriented (maintainable and extendable) solution for socket
communication that uses the most efficient approach for a given
platform (IO completion ports with thread pool on Win2000, something
else for Linux)?

ACE

http://www.cs.wustl.edu/~schmidt/ACE-overview.html

-cd
 
Back
Top