简介

一直说, emplace_back 比 push_back 快, 我不信, 哈哈~~

参考链接

https://blog.csdn.net/yockie/article/details/52674366

https://www.zhihu.com/question/64493068

移动构造函数

using emplace_back avoids the extra copy or move operation required when using push_back(简单来说确实避免了移动构造函数.)

移动构造函数是C++11带来的std::move强制将左值转为右值, 简单来说就是, 你有一个指针对象, 当使用拷贝构造函数的时候, 消耗太大, 你可以只用移动构造函数, 将指针进行浅拷贝, 然后,就可以了, 这样带来性能的极大提高.

code

线上完整代码

#include <vector>
#include <string>
#include "time.hh" class Foo {
public:
Foo(std::string str) : name(str) {
std::cout << "constructor" << std::endl;
}
Foo(const Foo& f) : name(f.name) {
std::cout << "copy constructor" << std::endl;
}
Foo(Foo&& f) : name(std::move(f.name)){
std::cout << "move constructor" << std::endl;
} private:
std::string name;
};
int main() { std::vector<Foo> v;
int count = 10000000;
v.reserve(count); //预分配十万大小,排除掉分配内存的时间
const int num = 100;
{
TIME_INTERVAL_SCOPE("push_back T:");
Foo temp("ceshi645231");
for(int i=0; i<num; i++) {
v.push_back(temp);// push_back(const T&),参数是左值引用
} //打印结果:
//constructor
//copy constructor
}
std::cout << "111111111111111111111\n";
v.clear();
{
TIME_INTERVAL_SCOPE("push_back move(T):");
Foo temp("ceshi645321");
for(int i=0; i<num; i++) { v.push_back(std::move(temp));// push_back(T &&), 参数是右值引用
} //打印结果:
//constructor
//move constructor
}
std::cout << "222222222222222222222\n";
v.clear();
{
TIME_INTERVAL_SCOPE("push_back(T&&):");
for(int i=0; i<num; i++) {
v.push_back(Foo("ceshi654321"));// push_back(T &&), 参数是右值引用
} //打印结果:
//constructor
//move constructor
}
std::cout << "333333333333333333333\n";
v.clear();
{
std::string temp = "ceshi123456";
TIME_INTERVAL_SCOPE("push_back(string):");
for(int i=0; i<num; i++) {
v.push_back(temp);// push_back(T &&), 参数是右值引用
} //打印结果:
//constructor
//move constructor
}
std::cout << "444444444444444444444444444\n";
v.clear();
{
std::string temp = "ceshi123465";
TIME_INTERVAL_SCOPE("emplace_back(string):");
for(int i=0; i<num; i++) {
v.emplace_back(temp);// 只有一次构造函数,不调用拷贝构造函数,速度最快
}
//打印结果:
//constructor
}
}
#pragma once

#include <iostream>
#include <memory>
#include <string>
#ifdef GCC
#include <sys/time.h>
#else
#include <ctime>
#endif // GCC class TimeInterval
{
public:
TimeInterval(const std::string& d) : detail(d)
{
init();
} TimeInterval()
{
init();
} ~TimeInterval()
{
#ifdef GCC
gettimeofday(&end, NULL);
std::cout << detail
<< 1000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000
<< " ms" << endl;
#else
end = clock();
std::cout << detail
<< (double)(end - start) << " ms" << std::endl;
#endif // GCC
} protected:
void init() {
#ifdef GCC
gettimeofday(&start, NULL);
#else
start = clock();
#endif // GCC
}
private:
std::string detail;
#ifdef GCC
timeval start, end;
#else
clock_t start, end;
#endif // GCC
}; #define TIME_INTERVAL_SCOPE(d) std::shared_ptr<TimeInterval> time_interval_scope_begin = std::make_shared<TimeInterval>(d)

测试结果

constructor
copy constructor
...
copy constructor
push_back T:922 ms
111111111111111111111
constructor
move constructor
...
move constructor
push_back move(T):893 ms
222222222222222222222
constructor
move constructor
constructor
...
constructor
move constructor
constructor
move constructor
push_back(T&&):1794 ms
333333333333333333333
constructor
move constructor
...
move constructor
constructor
move constructor
push_back(string):1884 ms
444444444444444444444444444
constructor
...
constructor
emplace_back(string):933 ms

emplace_back VS push_back的更多相关文章

  1. C++11使用emplace_back代替push_back

    最近在写一段代码的时候,突然很好奇C++11中对push_back有没有什么改进以增加效率,上网搜了一些资料,发现果然新增了emplace_back方法,比push_back的效率要高很多. 首先,写 ...

  2. 学习 emplace_back() 和 push_back 的区别 emplace_back效率高

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

  3. 编程杂谈——使用emplace_back取代push_back

    近日在YouTube视频上看到关于vector中emplace_back与push_back区别的介绍,深感自己在现代C++中还是有不少遗漏的知识点,遂写了段代码,尝试比较两者的差别. 示例代码 #i ...

  4. emplace_back与push_back的区别

    std::vector::emplace_back     C++   Containers library   std::vector   template< class... Args &g ...

  5. C++11 vector使用emplace_back代替push_back

    C++11中,针对顺序容器(如vector.deque.list),新标准引入了三个新成员:emplace_front.emplace和emplace_back,这些操作构造而不是拷贝元素.这些操作分 ...

  6. emplace_back() 和 push_back 的区别(转)

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

  7. (转)C++11使用emplace_back代替push_back (其中有关于右值引用)

    最近在写一段代码的时候,突然很好奇C++11中对push_back有没有什么改进以增加效率,上网搜了一些资料,发现果然新增了emplace_back方法,比push_back的效率要高很多. 首先,写 ...

  8. C++ std::vector emplace_back 优于 push_back 的理由

    #include <iostream> #include <vector> #include <chrono> #include <windows.h> ...

  9. 【C/C++开发】emplace_back() 和 push_back 的区别

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

  10. emplace_back() 和 push_back 的区别

    在引入右值引用,转移构造函数,转移复制运算符之前,通常使用push_back()向容器中加入一个右值元素(临时对象)的时候,首先会调用构造函数构造这个临时对象,然后需要调用拷贝构造函数将这个临时对象放 ...

随机推荐

  1. public boolean add(E e)的源码分析

    public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess ...

  2. robotframework-python3安装指南

    参考https://blog.csdn.net/ywyxb/article/details/64126927 注意:无论是在线还是离线安装,最好在管理员权限下执行命令 1.安装Python36(32位 ...

  3. Golang解决fatal error: all goroutines are asleep - deadlock!

    今天进行一个协程操作demo时总是报错 //workerpool.go package main import ( "fmt" "time" ) //工作线程 ...

  4. php获取前一天,前一个月,前半年,前一年的时间戳

    #获取前一小时strtotime("-1 hour") #获取前一天strtotime("-1 day") #获取前一周strtotime("-1 w ...

  5. 基于Cherry Studio + DeepSeek 搭建本地私有知识库!

    在当今数字化时代,知识管理变得越来越重要.无论是个人还是企业,都希望能够高效地存储.管理和检索知识.而借助 AI 技术,我们可以实现更加智能的知识库系统.本文将详细介绍如何使用 Cherry Stud ...

  6. AD 侦查-MSRPC Over SMB

    本文通过 Google 翻译 AD Recon – MSRPC Over SMB (135/139/445) 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校正及个别注释补充. 导航 0 ...

  7. python ast模块使用

    ast(Abstract Syntax Trees)是python中非常有用的一个模块,我们可以通过分析python的抽象语法树来对python的代码进行分析和修改. ast作用在python代码的语 ...

  8. VS2019 配置 protobuf3.8.0

    1.下载protobuf3.8.0 https://github.com/protocolbuffers/protobuf/releases/tag/v3.8.0 2.准备工作 解压文件并在同级目录建 ...

  9. Delegate的Target,Method

    在 C# 中,Delegate 是一种引用方法的类型,可以将方法视为对象进行传递和操作.Delegate 类型的实例可以用来引用一个或多个方法,然后可以将这些引用作为参数传递给其他方法,或者用来调用这 ...

  10. codeup之【字符串】回文串

    题目描述 读入一串字符,判断是否是回文串."回文串"是一个正读和反读都一样的字符串,比如"level"或者"noon"等等就是回文串. 输入 ...