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 ...
随机推荐
- Eclipse+Maven+TestNg+ReportNg 生成测试报告
http://blog.csdn.net/a542551042/article/details/46729585
- SQL2005恢复只有mdf文件的数据库
我把原来的数据库分离后,直接把日志文件给干掉了.原来在SQL 2000里经常这么干,只用一个mdf就附加了.没想到sql2005居然不行.我试验了一圈 终于找到一个成功的方法.转载,供后来者参考. S ...
- jquery操作select(option)的取值,设置和选中
比如 <select class="selector"> <option value ="volvo">Volvo</option ...
- Codeforces 61B【怪在读题】
搞不懂为什么DFS的写法崩了,然后乱暴力,因为题意不是很懂... 主要还是读题吧(很烦 #include <bits/stdc++.h> using namespace std; type ...
- lightoj 1096【矩阵快速幂(作为以后的模板)】
基础矩阵快速幂何必看题解 #include <bits/stdc++.h> using namespace std; /* 0 1 2 3 4 5 6 7 0 0 0 */ const i ...
- hdu1158【DP】
题意: 第一行项目数: 第二行每个工人的Hire Salary Fire money 第三行每个项目需要的人的数量: 工人在hire/fire的时候要付出额外的钱,如果已经hire了还没有fire就一 ...
- perl C/C++ 扩展(一)
通过h2xs 中间件,我们可以快速的使用c或则C++ 库来实现perl 扩展功能 第一讲:跑通hello world 程序******************************我们使用命令:h2 ...
- [題解]luogu_P3205/BZOJ_1996 合唱隊
前言:基本上發題解的都是抄的題解所以 來源:題解 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个 ...
- VMware每次联网都需要还原默认设置解决办法
参考:https://zhidao.baidu.com/question/553464573715382812.html
- Sql 2000系统表 语句查询表结构
SQL2000系统表的应用 –1:获取当前数据库中的所有用户表 select Name from sysobjects where xtype=’u’ and status>=0 –2:获取 ...