C ++ 0x性能改进 [英] C++0x performance improvements

查看:163
本文介绍了C ++ 0x性能改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以编写更高效的C ++代码的C ++ 0x的改进之一是unique_ptr智能指针(太糟糕了,它不允许像memoove()这样的操作:提案没有变成



$ b

下一个标准的其他效果改进是什么? $ b

 矢量< char *> v(10,astring); 
string concat = accumulate(v.begin(),v.end(),string());

代码将连接向量 v 中包含的所有字符串。这个整洁的代码的问题是,accumulate()复制的东西,而不使用引用。并且string()重新分配每次调用plus运算符。



C ++ 0x是否提供了解决问题的工具,也许是其他人?

解决方案

是C ++通过称为 move semantics 的方法解决问题。



基本上,它允许一个对象采用另一个对象的内部表示,如果该对象是临时的。例如,不是通过复制构造函数复制字符串中的每个字节,而是通常只允许目标字符串接受源字符串的内部表示。只有当源是r值时才允许这样做。



这是通过引入一个 move constructor 来实现的。它的一个构造函数,你知道src对象是一个临时的,并将要走了。因此,目的地接受src对象的内部表示是可以接受的。



对于移动赋值运算符 / p>

为了区别复制构造函数和移动构造函数,语言引入了 rvalue references 。类定义了它的move构造函数,以获取一个只会绑定到右值(临时)的右值引用。所以我的类将定义一些符合以下行的:

  class CMyString 
{
private:
char * rawStr;
public:

//移动构造函数绑定到rvalues
CMyString(CMyString&& srcStr)
{
rawStr = srcStr.rawStr
srcStr.rawStr = NULL;
}

//移动赋值运算符
CMyString& operator =(CMyString& srcStr)
{
if(rawStr!= srcStr.rawStr)//防止自我分配
{
delete [] rawStr;
rawStr = srcStr.rawStr
srcStr.rawStr = NULL;
}
return * this;
}

〜CMyString()
{
delete [] rawStr;
}
}

这是一个非常好的和详细的文章关于移动语义以及允许您执行此操作的语法。


One of the C++0x improvements that will allow to write more efficient C++ code is the unique_ptr smart pointer (too bad, that it will not allow moving through memmove() like operations: the proposal didn't make into the draft).

What are other performance improvements in upcoming standard? Take following code for example:

vector<char *> v(10,"astring");
string concat = accumulate(v.begin(),v.end(), string(""));

The code will concatenate all the strings contained in vector v. The problem with this neat piece of code is that accumulate() copies things around, and does not use references. And the string() reallocates each time plus operator is called. The code has therefore poor performance compared to well optimized analogical C code.

Does C++0x provide tools to solve the problem and maybe others?

解决方案

Yes C++ solves the problem through something called move semantics.

Basically it allows for one object to take on the internal representation of another object if that object is a temporary. Instead of copying every byte in the string via a copy-constructor, for example, you can often just allow the destination string to take on the internal representation of the source string. This is allowed only when the source is an r-value.

This is done through the introduction of a move constructor. Its a constructor where you know that the src object is a temporary and is going away. Therefore it is acceptable for the destination to take on the internal representation of the src object.

The same is true for move assignment operators.

To distinguish a copy constructor from a move constructor, the language has introduced rvalue references. A class defines its move constructor to take an rvalue reference which will only be bound to rvalues (temporaries). So my class would define something along the lines of:

 class CMyString
 {
 private:
     char* rawStr;
 public:

     // move constructor bound to rvalues
     CMyString(CMyString&& srcStr) 
     {
         rawStr = srcStr.rawStr
         srcStr.rawStr = NULL;             
     }

     // move assignment operator 
     CMyString& operator=(CMyString&& srcStr) 
     {
         if(rawStr != srcStr.rawStr) // protect against self assignment
         {
             delete[] rawStr;
             rawStr = srcStr.rawStr
             srcStr.rawStr = NULL;
         }
         return *this;
     }

     ~CMyString()
     {
         delete [] rawStr;
     }
 }

Here is a very good and detailed article on move semantics and the syntax that allows you to do this.

这篇关于C ++ 0x性能改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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