Hi,
I've several methods which get large objects as parameters like:
qr_decompositio n(const DenseMatrix &A)
My question is simple: Should the line above be the preferred way or
should the parameter be given like
qr_decompositio n(DoubleDenseMa trix A)
I know the difference in semantics. But does it make some essential
difference
for the compiler? Is it better to have the compiler do the copy or to do it
by oneself inside the method? Any performance differences?
regards,
alex 9 3976
Alexander Stippler wrote: Hi,
I've several methods which get large objects as parameters like:
qr_decompositio n(const DenseMatrix &A)
My question is simple: Should the line above be the preferred way or should the parameter be given like
qr_decompositio n(DoubleDenseMa trix A)
I know the difference in semantics. But does it make some essential difference for the compiler? Is it better to have the compiler do the copy or to do it by oneself inside the method? Any performance differences?
regards, alex
If you have to copy the object, it does not really matter. If you do not
need to copy it inside the function use the const-ref form.
--
WW aka Attila
:::
There are wo kinds of people: Those who finish what they start
"Alexander Stippler" <st**@mathemati k.uni-ulm.de> wrote in message
news:40******@n ews.uni-ulm.de... Hi,
I've several methods which get large objects as parameters like:
qr_decompositio n(const DenseMatrix &A)
My question is simple: Should the line above be the preferred way or should the parameter be given like
qr_decompositio n(DoubleDenseMa trix A)
I know the difference in semantics. But does it make some essential difference for the compiler?
Typically yes, the generated code for the two forms will be different.
Is it better to have the compiler do the copy or to do it by oneself inside the method? Any performance differences?
If the object is 'large', (as you state above), then imo pass
by reference should be preferred. But the only conclusive way
to find out about performance is to measure.
-Mike
"White Wolf" <wo***@freemail .hu> wrote in message
news:bv******** **@phys-news1.kolumbus. fi... Alexander Stippler wrote: Hi,
I've several methods which get large objects as parameters like:
qr_decompositio n(const DenseMatrix &A)
My question is simple: Should the line above be the preferred way
or should the parameter be given like
qr_decompositio n(DoubleDenseMa trix A)
I know the difference in semantics. But does it make some
essential difference for the compiler? Is it better to have the compiler do the copy or
to do it by oneself inside the method? Any performance differences?
regards, alex If you have to copy the object, it does not really matter. If you
do not need to copy it inside the function use the const-ref form.
Andrei Alexandrescu (of "Modern C++ Design") has argued that passing
by const ref when you need to make a copy internally is less efficient
than passing by value. See http://lists.boost.org/MailArchives/boost/msg10536.php.
Jonathan
In article <40******@news. uni-ulm.de>, Alexander Stippler wrote: Hi,
I've several methods which get large objects as parameters like:
qr_decompositio n(const DenseMatrix &A)
My question is simple: Should the line above be the preferred way or should the parameter be given like
qr_decompositio n(DoubleDenseMa trix A)
I know the difference in semantics. But does it make some essential difference for the compiler? Is it better to have the compiler do the copy or to do it by oneself inside the method? Any performance differences?
I think it's better to use const reference, because otherwise, you are
"disclosing " the fact that you deep-copy in the interface, but this should
be an implementation detail. The client code should not know whether or not
you deep-copy.
It's actually not hard to see how one could "implement" this without a deep
copy. For example, you could further down the road use a third party library
to do it instead, in which case the "copying" would involve glue code that
converted your matrix into a data structure the other library could use.
The example may seem contrived or non-applicable, but it also illustrates that
one signature imposes more constraints on implementation than the other.
Cheers,
--
Donovan Rebbechi http://pegasus.rutgers.edu/~elflord/
Alexander Stippler wrote: I've several methods which get large objects as parameters like:
QRDecomposition qr_decompositio n(const DoubleDenseMatr ix &A);
My question is simple: Should the line above be the preferred way? Or should the parameter be given like
QRDecomposition qr_decompositio n(DoubleDenseMa trix A);
I know the difference in semantics. But does it make some essential difference for the compiler?
These two definitions are equivalent
as far as the calling program is concerned.
Is it better to have the compiler do the copy Or to do it by oneself inside the method?
It doesn't matter if you implementation calls for a copy to be made.
Any performance differences?
No.
Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library (SVMTL) http://www.netwood.net/~edwin/svmtl/
(svmtl/src/matrix/Matrix.ccP: line #481-517)
Most implementations [in any computer programming language]
provide a method to perform the decomposition *in-place*:
DoubleDenseMatr ix& qr_decompositio n(DoubleDenseMa trix &A);
The QR decomposition is "packed" back into DoubleDenseMatr ix A
in a format defined be the implementation
(and, in this case, a reference to A is returned).
This isn't a very good Application Program Interface (API)
because a DoubleDenseMatr ix object has effectively
been converted into a QRDecomposition object
and is no longer a meaningful DoubleDenseMatr ix object.
Obviously, this will certainly lead some unwary application programmer
to grief.
Donovan Rebbechi wrote: I think it's better to use const reference because, otherwise, you are "disclosing " the fact that you deep-copy in the interface but this should be an implementation detail. The client code should not know whether or not you deep-copy.
Please show us an example of a "client" program
that can tell whether the function was declared
int f(int);
or
int f(const int&);
It's actually not hard to see how one could "implement" this without a deep copy. For example, you could, further down the road, use a third party library to do it instead, in which case the "copying" would involve glue code that converted your matrix into a data structure the other library could use.
The example may seem contrived or non-applicable but it also illustrates that one signature imposes more constraints on implementation than the other.
You can simply substitute an f(const int&) declaration
for the f(int) declaration in your header file.
The only "constraint " is that
you would need to recompile the application.
In article <40************ @jpl.nasa.gov>, E. Robert Tisdale wrote: Donovan Rebbechi wrote:
Please show us an example of a "client" program that can tell whether the function was declared
int f(int);
or
int f(const int&);
[snip] The only "constraint " is that you would need to recompile the application.
You're right, but some people actually think binary compatibility is important.
Even if this is only a small advantage, the pass-by-const reference doesn't
have any disadvantages (or did I miss something)
Cheers,
--
Donovan Rebbechi http://pegasus.rutgers.edu/~elflord/
"Donovan Rebbechi" <ab***@aol.co m> wrote in message: Even if this is only a small advantage, the pass-by-const reference
doesn't have any disadvantages (or did I miss something)
Supposedly, it may inhibit some optimizations. See http://lists.boost.org/MailArchives/boost/msg10536.php.
Jonathan
Donovan Rebbechi wrote: Even if this is only a small advantage, the pass-by-const reference doesn't have any disadvantages (or did I miss something)
int f(const int&);
It's probably best to pass small objects by value
instead of by const reference. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mangi03 |
last post by:
Hi,
I came acrosss g++ compile errors whenever I make a function call by
reference and found out from the test program that compiler is
treating the function argument differently when another function call
funcRet()is made which returns the expected argument type for the
function call by reference funcByRef(class A&);
The only way to get around this probelm is to first call the
funcRet(), assign its value to a variable and pass that...
|
by: Lateralus |
last post by:
headers/vector3.h: In member function ‘Vector3
Vector3::operator*(Scalar)’:
headers/vector3.h:13: error: no matching function for call to
‘Vector3::Vector3(Vector3)’
‘Vector3::Vector3(Vector3)’
headers/vector3.h:10: note: candidates are: Vector3::Vector3(Vector3&)
I'm a little confused as to what my problem is. I get this error for
any method which calls the constructor.
|
by: homsan toft |
last post by:
Hi,
I'm (still) trying to return a pair<const Key, T> from
iterator dereference. So I defined a proxy class in the
obvious way:
template<class KeyT, class DataT>
struct ref_proxy
{
typedef const KeyT first_type;
|
by: priyasmita_guha |
last post by:
C uses call by value for passing of parameters in contrast to C++ which
uses call by reference.How is call by value advantageous and how is it
implemented?
|
by: Eric Lilja |
last post by:
Hello, consider this complete program:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
class Hanna
{
| |
by: uday.sen |
last post by:
Hi,
I am porting a piece of code from VC++ to linux platform. My compiler
is g++ 3.2.2. I am getting following error:
no matching function for call to A::setResponse(std::wstring)
candidates are A::setResponse(std::wstring &) ---> This is
indeed the signature
I am using this function as:
|
by: Michael Safyan |
last post by:
Dear members of comp.lang.c++,
I am a little bit confused about the differences between constant
references and values. I understand that it is faster to use a constant
reference ("const T&") than a value ("T") since the former does not
require copying whereas the latter does, is that correct? Also, I un
derstand that "const T&" allows for polymorphism whereas "T" will
generate code cuttting.
On the former points, I am fairly confident......
|
by: Frederick Gotham |
last post by:
We all know that there's special rules for the binding of a "reference to
const" to an R-value, i.e.:
int const &i = 5;
acts as if it were:
int const __rval = 5;
int const &i = __rval;
|
by: Zytan |
last post by:
This returns the following error:
"Cannot modify the return value of
'System.Collections.Generic.List<MyStruct>.this' because it is
not a variable"
and I have no idea why! Do lists return copies of their elements?
Why can't I change the element itself?
class Program
{
private struct MyStruct
|
by: cablepuff |
last post by:
template <typename ContainerType>
ContainerType rsa_encrypt_list(const std::string&, const typename
ContainerType::reference, const typename ContainerType::reference);
const BigInteger e(boost::lexical_cast<BigInteger>(rsa_encrypts));
const BigInteger
n(boost::lexical_cast<BigInteger>(rsa_encrypts));
std::string infile(rsa_encrypts);
boost::scoped_ptr<boost::filesystem::ifstreamencrypt_input(new
boost::filesystem::ifstream(infile));
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |