问:通过几个函数调用返回值优化? [英] Q: Return value optimization thru several function calls?

查看:84
本文介绍了问:通过几个函数调用返回值优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


我使用的是VC ++ 7.1并且对返回值优化有疑问。

请考虑以下代码:


#include< list>

#include< string>


struct test

{

std :: list< std :: string> m_strings;

};


test func1(int r)

{

if(r == 0)

返回测试();


测试t = func1(r - 1);

t.m_strings .push_back(" lala");

返回t;

}


int main()

{

test t = func1(3);

}


现在,单步执行代码,假设''test''对象是经常构造和销毁的
副本。我的问题是,像VC ++ 7.1一样,一个像样的

编译器能够以这样一种方式对其进行优化,即只使用一个单独的测试对象整个代码?为了使这篇文章更具主题,标准甚至会允许进行这样的优化,因为这是对原始代码的一个相当大的改变。


提前致谢!

-

jb


(如果用x取代y你想通过电子邮件回复)

Hi!

I am using VC++ 7.1 and have a question about return value optimization.
Consider the following code:

#include <list>
#include <string>

struct test
{
std::list <std::string> m_strings;
};

test func1 (int r)
{
if (r == 0)
return test ();

test t = func1 (r - 1);
t.m_strings.push_back ("lala");
return t;
}

int main ()
{
test t = func1 (3);
}

Now, stepping thru the code, one would assume that ''test'' objects are
copy constructed and destroyed often. My question is, would a decent
compiler, like VC++ 7.1, be able to optimize this in such a way that only a
single ''test'' object is used throughout the code? To make this post more
on-topic, would the Standard even allow such an optimization, as it is a
pretty great change to the original code ..

Thanks in advance!
--
jb

(replace y with x if you want to reply by e-mail)

推荐答案

Jakob Bieling写道:
Jakob Bieling wrote:
嗨!
请考虑以下代码:

#include< list>
#include< string>

struct test
{st / :: std :: list< std :: string> m_strings;
};

测试func1(int r)
{
if(r == 0)
return test();

测试t = func1(r - 1);
t.m_strings.push_back(" lala");
返回t;
}
int main()
{
测试t = func1(3);
}
现在,通过代码,人们会认为''测试' '对象经常被复制构造和销毁。我的问题是,一个像VC ++ 7.1这样的体面编译器是否能够以这样的方式对其进行优化:在整个代码中只使用单个测试对象?为了使这篇文章更具主题性,标准甚至会允许进行这样的优化,因为它对原始代码进行了相当大的改动..
Hi!

I am using VC++ 7.1 and have a question about return value optimization.
Consider the following code:

#include <list>
#include <string>

struct test
{
std::list <std::string> m_strings;
};

test func1 (int r)
{
if (r == 0)
return test ();

test t = func1 (r - 1);
t.m_strings.push_back ("lala");
return t;
}

int main ()
{
test t = func1 (3);
}

Now, stepping thru the code, one would assume that ''test'' objects are
copy constructed and destroyed often. My question is, would a decent
compiler, like VC++ 7.1, be able to optimize this in such a way that only a
single ''test'' object is used throughout the code? To make this post more
on-topic, would the Standard even allow such an optimization, as it is a
pretty great change to the original code ..



标准允许优化(6.3.3)。



The standard allows the optimization (6.3.3).


" Jeff Schwab" < JE ****** @ comcast.net>在消息中写道

news:bt ******************** @ comcast.com ......
"Jeff Schwab" <je******@comcast.net> wrote in message
news:bt********************@comcast.com...
标准允许优化(6.3.3)。
The standard allows the optimization (6.3.3).




在我的副本中,6.3是复合语句或块。并且只有一个

段落。 IOW,没有6.3.3?


问候

-

jb


(如果你想通过电子邮件回复,用x替换y)



In my copy, 6.3 is "Compound statement or block" and only has a single
paragraph. IOW, there is no 6.3.3?

regards
--
jb

(replace y with x if you want to reply by e-mail)


Jakob Bieling写道:
Jakob Bieling wrote:

你好!

我正在使用VC ++ 7.1并对返回值优化有疑问。
请考虑以下代码:

#include< list>
#include< string>

结构测试
{st / :: std :: list< std :: string> m_strings;
};

测试func1(int r)
{
if(r == 0)
return test();

测试t = func1(r - 1);
t.m_strings.push_back(" lala");
返回t;
}
int main()
{
测试t = func1(3);
}

Hi!

I am using VC++ 7.1 and have a question about return value optimization.
Consider the following code:

#include <list>
#include <string>

struct test
{
std::list <std::string> m_strings;
};

test func1 (int r)
{
if (r == 0)
return test ();

test t = func1 (r - 1);
t.m_strings.push_back ("lala");
return t;
}

int main ()
{
test t = func1 (3);
}




正如杰夫所说,在上面例如,允许编译器优化

远离不必要的本地(在func1()),临时和相应的

复制构造函数调用。如果func1()定义如下:


test func1(int r)

{

test t;

t.m_strings.push_back(" lala");

返回t;

}


with相同的main(),我肯定会期待一个体面的编译器只有一个构造和

破坏。然而,递归可能会使某些事情复杂化。您可以添加几个ctors进行测试(默认和复制)和

a dtor cout a message and see。


考虑一下:

int main()

{

test t;

//做点什么......


t = func1(3);

}


在这种情况下运气不大:t将首先构建,然后在至少一个

类测试的对象将被创建为func1(),然后operator =

将被调用。


当我关心性能时,我通常将func1()定义为

void func1(int r,test& t){...}

如果功能表示法很重要,我会考虑使用代理

(并且相应地容纳实际对象),但我不会从函数返回

真正的大对象。 />

Denis



As Jeff said, in the above example the compiler is allowed to optimise
away unnecessary locals (in func1()), temporaries and the corresponding
copy constructor calls. If func1() were defined like this:

test func1 (int r)
{
test t;
t.m_strings.push_back ("lala");
return t;
}

with the same main(), I would definitely expect just one construction and
destruction from a decent compiler. The recursion, however, may complicate
things a bit. You can add a couple of ctors to test (default and copy) and
a dtor that cout a message and see.

Consider this too:
int main()
{
test t;
//do something to t ...

t = func1 (3);
}

Not much luck in this case: t will be constructed first, then at least one
object of class test will be created as a result of func1(), then operator =
will be called.

When I care about performance, I usually define func1() as
void func1 (int r, test& t) {...}
If the functional notation were important, I would consider using proxies
(and accomodate the "actual" objects acordingly), but I would not return
really big objects from a function.

Denis


这篇关于问:通过几个函数调用返回值优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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