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 ...
随机推荐
- window.showModalDialog 在谷歌Uncaught TypeError: undefined is not a function
if(navigator.userAgent.indexOf("Chrome") >0 ){var winOption = "height="+heigh ...
- MarketServer 日志
2014.04.29 1. 发现有时候会跳出 Exception Infomations: 用户异常信息:Socket未连接 跟踪后发现的一次情况是: 服务器根据客户端请求从后台读取数据后,写数据 ...
- IT兄弟连 JavaWeb教程 JSTL常用标签
1.条件标签 条件标签能够实现Java语言中的if语句以及if-else语句的功能,它包括以下几种: <c:if>:用于实现Java语言中的if语句的功能. <c:choose> ...
- IT兄弟连 JavaWeb教程 JSP定义
JSP页面是指扩展名为.jsp的文件,在一个JSP页面中,可以包括指令标识.HTML代码.JavaScript代码.嵌入的Java代码.注释和JSP动作标识等内容.但这些内容并不是一个JSP页面所必须 ...
- iOS中UIWebView使用JS交互
iOS中偶尔也会用到webview来显示一些内容,比如新闻,或者一段介绍.但是用的不多,现在来教大家怎么使用js跟webview进行交互. 这里就拿点击图片获取图片路径为例: 1.测试页面html & ...
- Spring源码(一)
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</lis ...
- 彻底剖析RMI底层源码 、手写轻量级RMI框架
https://blog.csdn.net/Z0157/article/details/82049975 User public class User { private int age; publi ...
- nginx 一些配置
worker_processes 4; #工作进程数 events { #epoll是多路复用IO(I/O Multiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高ng ...
- C# 委托之把委托从委托链(多播委托)移除
运用“-”运算符将委托从委托链移除 class HelloWorld { //定义委托类型 delegate void DelegationChain(); static void Main(stri ...
- Ubuntu-apt安装Jenkins
系统环境: Ubuntu 16.0.4 2CPU,8G 1.默认Ubuntu软件包里没有Jenkins 2.系统里添加存储密钥 wget -q -O - https://pkg.jenkins.io/ ...