前言
一直以来写代码,vector想都没想就是push_back,今天发现的C++11的emplace_back碾压了传统的push_back
总结一句话:Life is short, use emplace_back()。
简单叙述原因:emplace_back可以“同时构造和插入,一次搞定”,push_back需要“先构造,后插入,而且插入的时候还伴随着拷贝或者移动”。
Talk is cheap, show me the code
#include <iostream>
#include <utility>
#include <chrono>
#include <vector>
#include <string>
using namespace std;
using namespace chrono;
int main()
{
vector<string> v;
int num = 100000;
v.reserve(num);
cout << "push_back way one: ";
auto start = system_clock::now();
for (int i = 0; i < num; ++i)
{
string temp("caitao");
v.push_back(temp);
}
auto end = system_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
cout << "push_back way two: ";
v.clear();
start = system_clock::now();
for (int i = 0; i < num; ++i)
{
string temp("caitao");
v.push_back(move(temp));
}
end = system_clock::now();
duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
cout << "push_back way three: ";
v.clear();
start = system_clock::now();
for (int i = 0; i < num; ++i)
{
v.push_back(string("caitao"));
}
end = system_clock::now();
duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
cout << "push_back way four: ";
v.clear();
start = system_clock::now();
for (int i = 0; i < num; ++i)
{
v.push_back("caitao");
}
end = system_clock::now();
duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
cout << "emplace_back(fastest): ";
v.clear();
start = system_clock::now();
for (int i = 0; i < num; ++i)
{
v.emplace_back("caitao");
}
end = system_clock::now();
duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
分析
![push_back emplace_back](https://img-blog.csdn.net/20170510130633865?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzM5MDQ3Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第一种方法,速度最慢。push_back的参数是左值引用。首先定义一个temp对象的时候,调用一次构造函数,然后push_back的时候调用一次拷贝构造函数。两次构造很花时间,因为内存里面需要重新分配空间。
第二~四种方法,速度中等。push_back的参数是右值引用。首先定义一个temp对象的时候,调用一次构造函数,然后push_back的时候调用一次移动构造函数。移动构造函数花的时间比拷贝构造函数花的时间少,因为不需要内存重新分配空间。(不知道为什么第二种方法会比方法三和四花费的时间多一点点。。。)
第五种方法,即emplace_back速度最快,因为emplace_back只调用一次构造函数,没有移动构造函数,也没有拷贝构造函数。cplusplus.com说:Arguments forwarded to construct the new element. 意思是:emplace_back的参数就是构造函数的参数。
证明上述的分析
写一个类,重载构造函数,重载拷贝构造函数,重载移动构造函数。代码如下:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
struct Complicated
{
int year;
double country;
string name;
Complicated(int a, double b, string c) :year(a), country(b), name(c)
{
cout << "is constucted" << endl;
}
Complicated(const Complicated & other) : year(other.year), country(other.country), name(other.name)
{
cout << "is copied" << endl;
}
Complicated(Complicated && other) : year(move(other.year)), country(move(other.country)), name(move(other.name))
{
cout << "is moved" << endl;
}
};
int main()
{
int anInt = 4;
double aDouble = 5.0;
string aString = "C++";
vector<Complicated> v;
v.reserve(10);
cout << "--emplace_back--" << endl;
v.emplace_back(anInt, aDouble, aString);
cout << endl << "--push_back--" << endl;
Complicated temp(anInt, aDouble, aString);
cout << endl;
v.push_back(temp);
cout << endl;
v.push_back(move(temp));
cout << endl;
v.push_back(Complicated(anInt, aDouble, aString));
cout << endl;
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
输出结果如下:
![move_copy](https://img-blog.csdn.net/20160727112935406)
一种“更加公平”的比较方式
emplace_back可以“构造和插入同时进行”,堪称“开挂”。如果对象构造好了,只是单纯的比较插入(抛开外挂),谁快一些呢?
这次是“单纯”比较的push_back和emplace_back的插入功能:
#include <iostream>
#include <utility>
#include <chrono>
#include <vector>
#include <string>
using namespace std;
using namespace chrono;
int main()
{
vector<string> v;
int num = 100000;
v.reserve(num);
string caitao("caitao");
vector<string> storage(num, caitao);
cout << "push_back(copy): ";
auto start = system_clock::now();
for (int i = 0; i < num; ++i)
{
v.push_back(storage[i]);
}
auto end = system_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
cout << "push_back:(move): ";
v.clear();
start = system_clock::now();
for (int i = 0; i < num; ++i)
{
v.push_back(move(storage[i]));
}
end = system_clock::now();
duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
cout << "emplace_back: ";
v.clear();
start = system_clock::now();
for (int i = 0; i < num; ++i)
{
v.emplace_back(storage[i]);
}
end = system_clock::now();
duration = duration_cast<microseconds>(end - start);
cout << duration.count() << " microseconds." << endl << endl;
system("pause");
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
![emplace_back](https://img-blog.csdn.net/20170510142408560?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzM5MDQ3Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
emplace_back最快,push_back(move())次之,push_back最慢。所以呀,大家忘了push_back吧