C++11:移动构造函数的测试

代码如下:

#include <iostream>
#include <stddef.h>
#include <Windows.h> using namespace std; class CTest
{
private:
std::size_t size;
char* buffer; public:
CTest(std::size_t size) : size(size), buffer(nullptr)
{
buffer = new char[size];
} ~CTest()
{
delete[] buffer;
} CTest(const CTest& other) : size(other.size), buffer(nullptr)
{
buffer = new char[size];
} CTest(CTest&& other) : size(other.size), buffer(other.buffer)
{
other.buffer = nullptr;
}
}; template <typename Func>
__int64 Benchmark(Func f, size_t count)
{
// LARGE_INTEGER m_liPerfFreq={0};
// //获取每秒多少CPU Performance Tick
// QueryPerformanceFrequency(&m_liPerfFreq);
LARGE_INTEGER li = {};
QueryPerformanceCounter(&li);
__int64 start = li.QuadPart; f(count); QueryPerformanceCounter(&li);
return (li.QuadPart - start);//* 1000 / m_liPerfFreq.QuadPart;
} void Alloc_Ref(size_t count)
{
CTest t();
for(size_t i = ; i < count; ++i)
CTest c(t);
} void Alloc_Move(size_t count)
{
CTest t();
for(size_t i = ; i < count; ++i)
CTest c(std::move(t));
//CTest c(CTest(1024)); } int _tmain(int argc, _TCHAR* argv[])
{
for(int i= ; i< ;++i)
{
cout << "Move: "<< Benchmark(Alloc_Move, ) << " ms." << endl;
cout << "Ref: " << Benchmark(Alloc_Ref, ) << " ms." << endl;
} system("pause");
return ;
}

程序运行结果如下:

结论:可见移动构造函数是拷贝构造函数的1-3倍。

参考链接:

VS 2010, Move constructor only reached after move() and slower than copy constructor?

C++11:移动构造函数的测试的更多相关文章

  1. C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库

    C# DateTime的11种构造函数   别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...

  2. C++11中std::move、std::forward、左右值引用、移动构造函数的测试

    关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就 ...

  3. testng入门教程11 TestNG运行JUnit测试

    现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...

  4. 虚函数_构造函数_测试_VS2010x86

    1.控制台测试代码: #include <stdio.h> #include <windows.h> class A { public: A() { printf(" ...

  5. C# DateTime的11种构造函数

    别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Globalization; using Syste ...

  6. c++11 move构造函数和move operator 函数 学习

    先看个代码吧!!!!!!!!!! #include <iostream> using namespace std; class A { public: A(){cout<<&q ...

  7. [C++11] 默认构造函数

    类通过一个特殊的构造函数来控制默认初始化过程.这个函数就是默认构造函数.默认构造函数无需不论什么实參. 我们能够显示的定义默认构造函数也能够让编译器为我们生成默认构造函数. 默认构造函数以例如以下规则 ...

  8. c++11的构造函数继承

    https://en.cppreference.com/w/cpp/language/using_declaration 在[Inheriting constructors]这一节. 其实叫做"基类的 ...

  9. c++11 委派构造函数

    委派构造函数可以减少构造函数的书写量: class Info { public: Info() : type(), name('a') { InitRest(); } Info(int i) : ty ...

随机推荐

  1. angular6 render2 & viewContentRef实践

    angular 渲染层 angular一个跨平台的框架不仅仅针对的浏览器这一个平台 ElementRef 与 TemplateRef 简单的理解: ElemnetRef : 例如一个<span& ...

  2. 1090 Highest Price in Supply Chain (25 分)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

  3. apache2.4.35 403 forbidden 解决办法

  4. STLstack,queue

    今天一开始用C去摸栈和队列,差不多昨天早上也在摸,摸烦了就去搞DP然后DP也没搞好,就是很烦很烦!!!! 然后今天那些C的栈队列的步骤和名称熟的不要不要的,然而数据结构的c语言用指针,传递,简直麻烦, ...

  5. 一篇文章彻底弄懂CAS实现SSO单点登录原理

    1. CAS 简介 1.1. What is CAS ? CAS ( Central Authentication Service ) 是 Yale 大学发起的一个企业级的.开源的项目,旨在为 Web ...

  6. time库的使用

    首先只需要 import time (典型的,标准的python库的使用方法) 主要包括三类函数 ——时间获取:time() , ctime() , gmtime() ——时间格式化: strftim ...

  7. JavaScript 中的面向对象编程

    使用JSON 来定义一个对象: <script type="text/javascript">var xiaoming = { name : 'xiaoming', a ...

  8. 字符串匹配,KMP算法

    KMP的详解见:https://segmentfault.com/a/1190000008575379 主要难点在于Next数组的理解,KMP是不需要回溯的匹配算法. #include<iost ...

  9. canvas+js实现时钟效果图

    <! DOCTYPE html> <html> <head> <title>Clock</title> </head> < ...

  10. 牛客寒假5-A.炫酷双截棍

    链接:https://ac.nowcoder.com/acm/contest/331/A 题意: 小希现在手里有一个连着的两块木条,长度分别为l1l1,l2l2,木条之间有一个无摩擦的连接点,木条之间 ...