问题描述
我有几种方法可以将大对象作为参数来获取:
qr_decomposition(const DenseMatrix& A )
我的问题很简单:上面的行应该是首选方式还是
应该给出参数如
qr_decomposition(DoubleDenseMatrix A)
我知道语义上的差异。但它是否为编译器制作了一些必要的
差异
?编译器是否更好地复制或在方法中自行完成
?任何性能差异?
问候,
alex
Hi,
I''ve several methods which get large objects as parameters like:
qr_decomposition(const DenseMatrix &A)
My question is simple: Should the line above be the preferred way or
should the parameter be given like
qr_decomposition(DoubleDenseMatrix 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
推荐答案
如果你必须复制对象,它并不重要。如果你没有
需要在函数内部复制它,请使用const-ref表格。
-
WW aka Attila
:::
有很多人:完成他们开始的人
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
如果对象是大,(如上所述),则imo通过引用传递
应该是首选。但唯一可以确定性能的结论是确定性能。
-Mike
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
不需要在函数内部复制它,请使用const-ref表格。
do not need to copy it inside the function use the const-ref form.
Andrei Alexandrescu(属于现代C ++设计一书认为,当你需要在内部复制时,通过const ref传递
效率低于传递值的b $ b。参见
。
Jonathan
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
这篇关于按值/(常量)引用调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!