按值/(常量)引用调用 [英] call by value / (const) reference

查看:79
本文介绍了按值/(常量)引用调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有几种方法可以将大对象作为参数来获取:


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

推荐答案

Alexander Stippler写道:
Alexander Stippler wrote:


我有几种方法可以将大对象作为参数获得:

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




" Alexander Stippler" < ST ** @ mathematik.uni-ulm.de>在消息中写道

news:40 ****** @ news.uni-ulm.de ...

"Alexander Stippler" <st**@mathematik.uni-ulm.de> wrote in message
news:40******@news.uni-ulm.de...


我有几种方法可以将大对象作为参数来获取:

qr_decomposition(const DenseMatrix& A)

我的问题很简单:上面的行是否应该是首选方式或者
应该给出参数如何

qr_decomposition(DoubleDenseMatrix A)

我知道语义上的差异。但它是否为编译器带来了一些必要的差异?


通常是的,两个表单的生成代码会有所不同。

编译器执行复制或执行此操作是否更好?在方法里面自己?任何性能差异?
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?
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?




如果对象是大,(如上所述),则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




白狼 < WO *** @ freemail.hu>在消息中写道

news:bv ********** @ phys-news1.kolumbus.fi ...

"White Wolf" <wo***@freemail.hu> wrote in message
news:bv**********@phys-news1.kolumbus.fi...
Alexander Stippler写道:
Alexander Stippler wrote:


我有几种方法可以将大对象作为参数,例如:

qr_decomposition(const DenseMatrix& A)

我的问题很简单:上面的行应该是首选方式
还是应该给出参数

qr_decomposition(DoubleDenseMatrix A)

我知道语义上的差异。但它是否会为编译器带来一些
的本质区别?让编译器执行复制更好或者
在方法中自行完成吗?任何性能差异?

问候,
亚历克斯
如果你必须复制对象,它并不重要。如果您
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
If you have to copy the object, it does not really matter. If you



不需要在函数内部复制它,请使用const-ref表格。


do not need to copy it inside the function use the const-ref form.




Andrei Alexandrescu(属于现代C ++设计一书认为,当你需要在内部复制时,通过const ref传递

效率低于传递值的b $ b。参见
http://lists.boost.org/MailArchives /boost/msg10536.php


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


这篇关于按值/(常量)引用调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆