C++11:移动构造函数的测试
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:移动构造函数的测试的更多相关文章
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- C++11中std::move、std::forward、左右值引用、移动构造函数的测试
关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就 ...
- testng入门教程11 TestNG运行JUnit测试
现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...
- 虚函数_构造函数_测试_VS2010x86
1.控制台测试代码: #include <stdio.h> #include <windows.h> class A { public: A() { printf(" ...
- C# DateTime的11种构造函数
别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Globalization; using Syste ...
- c++11 move构造函数和move operator 函数 学习
先看个代码吧!!!!!!!!!! #include <iostream> using namespace std; class A { public: A(){cout<<&q ...
- [C++11] 默认构造函数
类通过一个特殊的构造函数来控制默认初始化过程.这个函数就是默认构造函数.默认构造函数无需不论什么实參. 我们能够显示的定义默认构造函数也能够让编译器为我们生成默认构造函数. 默认构造函数以例如以下规则 ...
- c++11的构造函数继承
https://en.cppreference.com/w/cpp/language/using_declaration 在[Inheriting constructors]这一节. 其实叫做"基类的 ...
- c++11 委派构造函数
委派构造函数可以减少构造函数的书写量: class Info { public: Info() : type(), name('a') { InitRest(); } Info(int i) : ty ...
随机推荐
- CodeForces - 767C Garland 树的遍历
C. Garland time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- POJ - 2251 Dungeon Master 多维多方向BFS
Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is ...
- HDU - 1016 Prime Ring Problem 经典素数环
Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...
- 4.1 手写Java PriorityQueue 核心源码 - 原理篇
本章先讲解优先级队列和二叉堆的结构.下一篇代码实现 从一个需求开始 假设有这样一个需求:在一个子线程中,不停的从一个队列中取出一个任务,执行这个任务,直到这个任务处理完毕,再取出下一个任务,再执行. ...
- android摄像头获取图像——第一弹
http://www.cnblogs.com/mengyan/archive/2012/09/01/2666636.html 安卓读取视频的几种方式: 详细讲述请参考网址:http://www.cnb ...
- Unity NGUI学习
环境 Unity4.3 NGUI v3.68 导入 Project界面->右键->import package->custom package载入安装包即可 untiy4.6用 ...
- bzoj 2406: 矩阵【二分+有源汇上下界可行流】
最大值最小,所以考虑二分 |Σaij-Σbij|<=mid,所以Σbij的上下界就是(Σaij-mid,Σaij+mid) 考虑建有上下界网络,连接(s,i,Σaik-mid,Σaik+mid) ...
- dosbox让DOSBox启动后自动执行命令——自动挂载
学习汇编时. 每次启动DOSBox后,都要挂载.转盘符,于是有一个骚套路. 找到这个:bat文件 用记事本就可以编辑. 拉到最下面,找到[autoexec]部分,补充命令如下: mount c f: ...
- ARP数据包
结构ether_header定义了以太网帧首部:结构arphdr定义了其后的5个字段,其信息用于在任何类型的介质上传送ARP请求和回答:ether_arp结构除了包含arphdr结构外,还包含源主机和 ...
- Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured.
在搭建Hadoop集群的时候,遇到了这样的一个报错. 就在启动HDFS调用命令: start-dfs.sh 的时候,报错: 然后输密码就一直很奇怪,反正一直运行不成功. 百度了半天,确定是core-s ...