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

From: Gianni Mariani (gi2nospam_at_mariani.ws)
Date: 03/30/05


Date: Wed, 30 Mar 2005 03:54:45 -0800

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.



Relevant Pages

  • Re: Optimizer bug (VC6 and VC7.1)?
    ... I think your code is undefined, so I'd hesitate to call it an optimizer bug. ... >This is the output with optimizations: ... You're binding the pointer produced by &x to a const reference. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Why is the memory doubled when writing data to files?
    ... > records using a header that indicates the size of the record, ... a reference to Fis allowed to bugger up a subsequent ... > The simple fact is that a modern Fortran implementation is big. ... > unbounded number of optimizations are possible. ...
    (comp.lang.fortran)
  • Re: Automatic (?) array bounds checking
    ... function allocates a temporary v3, ... copies the values (or probably a reference?) to the array c. ... In order for them to be valid optimizations, ... array is really large), ...
    (comp.lang.fortran)
  • Re: Did GC eat my mutex? Why did Mutex need to be static?
    ... How can the GC collect it when it's reference is not stored somewhere? ... to do with the compiler and compiler optimizations, ... In your case there seems to be a valid reference because ... > Application.Run doens't return until the app quits and so mutex is a valid ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Function prologs emitted by VC++ compiler
    ... see ebp being used as a frame pointer reliably. ... there is no hard guarantee that ebp will be used as a frame ... InterlockedIncrementwhere the prolog is optimized away and the ... When do compilers decide to do such prolog optimizations. ...
    (microsoft.public.win32.programmer.kernel)