STL中vector/list的几种赋值方法的速度比较

因为工作中遇到要对比较大的vector以及list进行比较频繁的互相复制的动作,为了提高赋值速度,所以对几种赋值方式的耗时进行了一下对比。

本文原来发表在CSDN博客上面,CSDN上内容现在已迁出至github page

测试代码:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include"stdafx.h"
#include<iostream>
#include<vector>
#include<list>
#include<algorithm>
#include<Windows.h>
usingnamespacestd;

typedefvector<int>vType;
typedeflist<int>lType;

constintTEST_MAX_LEN=100000;

int_tmain(intargc,_TCHAR*argv[])
{
vTypev1,v2;
lTypel1,l2;

//初始化v1和l1
for(inti=0;i<TEST_MAX_LEN;i++)
{
v1.push_back(i);
l1.push_back(i);
}

//初始化计时器
LARGE_INTEGERtmp;
LARGE_INTEGERnSart;
LARGE_INTEGERnEnd;

QueryPerformanceFrequency(&tmp);

intfreq=tmp.QuadPart;
if(0==freq)return-1;
//用赋值操作符赋值vector
{
v2.clear();
QueryPerformanceCounter(&nSart);
v2=v1;
QueryPerformanceCounter(&nEnd);

cout<<"用赋值操作符赋值vector:/t"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;
}

//用赋值操作符赋值list
{
l2.clear();
QueryPerformanceCounter(&nSart);
l2=l1;
QueryPerformanceCounter(&nEnd);

cout<<"用赋值操作符赋值list:/t"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;
}

//用assign给vector赋值
{
v2.clear();
QueryPerformanceCounter(&nSart);
v2.assign(v1.begin(),v1.end());
QueryPerformanceCounter(&nEnd);

cout<<"用assign给vector赋值:/t"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;
}

//用assign给list赋值
{
l2.clear();
QueryPerformanceCounter(&nSart);
l2.assign(l1.begin(),l1.end());
QueryPerformanceCounter(&nEnd);

cout<<"用assign给list赋值:/t"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;
}

//用copy算法给vector赋值(插入迭代器方式)
{
v2.clear();
QueryPerformanceCounter(&nSart);
copy(v1.begin(),v1.end(),inserter(v2,v2.begin()));
QueryPerformanceCounter(&nEnd);

cout<<"用copy算法给vector赋值(插入迭代器方式)"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;
}

//用copy算法给list赋值(插入迭代器方式)
{
l2.clear();
QueryPerformanceCounter(&nSart);
copy(l1.begin(),l1.end(),inserter(l2,l2.begin()));
QueryPerformanceCounter(&nEnd);

cout<<"用copy算法给list赋值(插入迭代器方式)"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;

}

//用copy算法给vector赋值(resize)
{
v2.clear();
QueryPerformanceCounter(&nSart);
v2.resize(v1.size());
copy(v1.begin(),v1.end(),v2.begin());
QueryPerformanceCounter(&nEnd);

cout<<"用copy算法给vector赋值(resize)"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;

}

//用copy算法给list赋值(resize)
{
l2.clear();
QueryPerformanceCounter(&nSart);
l2.resize(l1.size());
copy(l1.begin(),l1.end(),l2.begin());
QueryPerformanceCounter(&nEnd);

cout<<"用copy算法给list赋值(resize)"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;

}

//用assign给vector赋值(从list)
{
v2.clear();
QueryPerformanceCounter(&nSart);
v2.assign(l1.begin(),l1.end());
QueryPerformanceCounter(&nEnd);

cout<<"用assign给vector赋值(从list):/t"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;

}

//用assign给list赋值(从vector)
{
l2.clear();
QueryPerformanceCounter(&nSart);
l2.assign(v1.begin(),v1.end());
QueryPerformanceCounter(&nEnd);

cout<<"用assign给list赋值(从vector):/t"
<<(nEnd.QuadPart-nSart.QuadPart)*1000000/freq
<<endl;

}

getchar();
return0;

}

结果

测试结果

总结:

  1. vector对于vector赋值方式中
    • assign的速度是最快的
    • 其次是resize以后用copy算法赋值
    • 而最先能够想到的赋值操作符,速度却并不快,只能够排名第三,目前还不知道这是为什么
    • 采用插入迭代器再用copy的方式是速度最慢的一种。
  2. list对于list赋值
    • 赋值操作符的速度是最快的
    • 其次是assign
    • 然后是采用resize的copy
    • 最后一位同样是采用插入迭代子方式的copy。