效率。函数返回值与输出参数的关系 [英] Efficiency. Function return values versus output parameters

查看:180
本文介绍了效率。函数返回值与输出参数的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数返回值相对于输出参数,哪个更快?我想我最好使用我目前的工作解释。

  //指定标识符和返回指针。 
SceneNode * createSceneNode(const String& desired_identifier); // f1

//自动生成标识符并返回字符串。
String createSceneNode(SceneNode *& out_ptr_to_creation); // f2

//自动标识符和返回指向节点的指针
SceneNode * createSceneNode(String& out_gen_identifier); // f3

//自动生成标识符并返回字符串。
void createSceneNode(SceneNode *& out_ptr_to_creation,String& out_gen_identifier); // f4

我喜欢f1和f3,因为他们返回SceneNode *,它们会导致ambiguos调用。此外,通常只需要SceneNode *。字符串&在f3会出现一些不便和开销,所以我计划f1和f2。



我的问题是,是否会有一个区别

  f2(node); //未分配返回值。 
//会有一个优化不复制字符串?
mystring = f2(node);

我的猜测是函数参数存储应该在一些非常快速访问的区域,可用的输出参数工作更快。



我知道在我的例子中,这可能是最小的,但我只是想知道知识/兴趣的缘故。



另一个问题:我总是假设引用是32位数据,传递引用与指针一样快,是吗? / p>

谢谢。 =)

解决方案

之间的区别

  mystring = f2(node)

  f2(节点)

理解。在第一种情况下,大多数编译器将优化离开复制构造函数以返回值,并简单地从函数分配字符串,而不需要额外的复制步骤。在第二个版本的情况下,大多数优化器将完全省去任何副本或分配(根本没有任何分配,并且用于返回的副本构造函数可以被优化掉)。



您对f2 vs. f4的发言是准确的。由于返回优化,f2具有超过f4的良好机会。 (我会在找到它时添加一个链接到一个好的文章。)



哦,通过引用传递指针是很好。


Function return values versus "output" parameter, which one is faster? I think I best explain using what I am currently working on.

// specify identifier and return pointer.
SceneNode* createSceneNode(const String& desired_identifier);   // f1

// auto-gen identifier and return as string.
String createSceneNode(SceneNode*& out_ptr_to_creation); // f2

// auto-gen identifier and return pointer to node
SceneNode* createSceneNode(String& out_gen_identifier); // f3

// auto-gen identifier and return as string.
void createSceneNode(SceneNode*& out_ptr_to_creation, String& out_gen_identifier); // f4

While I prefer f1 and f3, because they return SceneNode*, they result in ambiguos call. Furthermore, often, only SceneNode* is required. The String& in f3 will present some inconvenience and overhead, so I am planning on f1 and f2.

My question is, will there be a difference between

f2(node); // return value not assigned.
          // will there be an optimisation NOT to copy string?
mystring = f2(node);

My guess is that function parameter storage should be in some very-fast-access area and will be readily available so output parameters work faster. however, if optimisation is done to prevent copy string in f2, then f2 will be better than f4.

I know in my example, this could be minimal, but I just want to know for knowledge/interest sake.

another side question: I always assume that references are 32bit data and passing reference is as fast as pointers, is that so?

Thanks. =)

解决方案

The difference between

mystring = f2(node)

and

f2(node)

are important to understand. In the first case, most compilers will optimize away the copy constructor for returning by value and simply assign the string from the function without the additional copy step. In the case of the second version, most optimizers will do away with any copy or assignment entirely (there's no assignment at all and the copy constructor for return can be optimized away).

Your statement about f2 vs. f4 is accurate. Because of return optimizations, f2 has a good chance of outperforming f4. (I'll add a link to a nice article when I find it.)

Oh, and passing pointers by reference is just fine.

这篇关于效率。函数返回值与输出参数的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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