Re: passing class to a function in c++ - performance question
Date: Wed, 30 Mar 2005 03:54:45 -0800
> 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.
- 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. ...
- 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. ...
- 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), ...
- 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 ...
- Practicality of microarchitecture-optimized register allocation?
... How practical would it be for a compiler to coordinate register ... this concept and similar optimizations might be applied to ... even specific hardware implementations under specific workloads. ...