zoukankan      html  css  js  c++  java
  • VC2010中C++的右值引用新特性

    // RightValue.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include <algorithm>商账追收
    #include <vector>
    #include <ctime>
    using namespace std;

    class CMyObj{ TCSTAR7034
    protected:
        unsigned m_iBufferSize;
        char* m_pBuffer;
    public:
        CMyObj():m_iBufferSize(0),m_pBuffer(NULL){}
        CMyObj(unsigned int iBufSize):
            m_iBufferSize(iBufSize)斜庞克发型怎么剪
            {
                m_pBuffer = new char[iBufSize];
                memset(m_pBuffer,0,m_iBufferSize);
            }
        CMyObj(const CMyObj& objSrc){
            m_iBufferSize = objSrc.m_iBufferSize;
            if(m_iBufferSize > 0){
                m_pBuffer = new char[m_iBufferSize];
                memcpy(m_pBuffer,objSrc.m_pBuffer,m_iBufferSize);
            }
        }
        ~CMyObj(){
            if(m_pBuffer) delete m_pBuffer;
            m_pBuffer = NULL;
        }
    };

    class CMyObjWithMoveConstructor:public CMyObj
    {
    public:
        CMyObjWithMoveConstructor():CMyObj(){}
        CMyObjWithMoveConstructor(int iBufSize)
            :CMyObj(iBufSize){}
        //copy constructor
        CMyObjWithMoveConstructor(const CMyObjWithMoveConstructor& myObj)
            :CMyObj(myObj){}

        //move constructor
        CMyObjWithMoveConstructor(CMyObjWithMoveConstructor&& myObj){
            m_iBufferSize = myObj.m_iBufferSize;
            m_pBuffer = myObj.m_pBuffer;

            myObj.m_pBuffer = NULL;
            myObj.m_iBufferSize = 0;
        }


    };

    int _tmain(int argc, _TCHAR* argv[])
    {
        static int iBufSize = 1024*1024*2; //2M
        static int iObjCnt = 100;

        clock_t t1,t2;
        double times;

        vector<CMyObj> vec;
        t1 = clock();
        //没有右值引用
        for( int i = 0; i < iObjCnt; i ++){ //加入100个元素
            vec.push_back( CMyObj(iBufSize));
        }
        t2 = clock();
        times = (double)(t2-t1)/CLOCKS_PER_SEC;
        cout<<"without move constructor:"<<times<<endl;
       
        vector<CMyObjWithMoveConstructor> vec2;
        t1 = clock();
        //有右值引用
        for( int j = 0; j < iObjCnt; j ++){ //加入100个元素
            vec2.push_back( CMyObjWithMoveConstructor(iBufSize));
        }
        t2 = clock();
        times = (double)(t2-t1)/CLOCKS_PER_SEC;
        cout<<"with move constructor:"<<times<<endl;

        return 0;
    }

    //最后结果:
    //without move constructor:1.48
    //with move constructor:0.16
    //相差了9.25倍
  • 相关阅读:
    Caffe2——C++ 预测(predict)Demo
    Effective C++ 条款06:若不想使用编译器自动生成的函数,就该明确拒绝
    Effective C++ 条款05:了解C++编写并调用哪些函数
    Effective C++ 条款04:确定对象被使用前已经先被初始化
    Effective C++ 条款03:尽可能使用const
    Effective C++ 条款02:尽量以const,enum,inline替换 #define
    使用队列(Queue)解决简单的并发问题
    关于C#中Queue的线程安全问题
    C#多线程编程
    跨线程访问控件的问题和编程方法
  • 原文地址:https://www.cnblogs.com/sky7034/p/2076037.html
Copyright © 2011-2022 走看看