测试对象类型不同,数量级不同时,表现具有差异:

测试数据对象为std::function时:

test: times(1000)
vector push_back time 469 us
vector emplace_back time 432 us
list push_back time 347 us
list emplace_back time 395 us
vector foreach time 29 us
list foreach time 24 us

test: times(10000)
vector push_back time 1459 us
vector emplace_back time 1344 us
list push_back time 816 us
list emplace_back time 885 us
vector foreach time 62 us
list foreach time 57 us

test: times(100000)
vector push_back time 11931 us
vector emplace_back time 12708 us
list push_back time 9575 us
list emplace_back time 8874 us
vector foreach time 626 us
list foreach time 711 us

test: times(1000000)
vector push_back time 110641 us
vector emplace_back time 109801 us
list push_back time 90353 us
list emplace_back time 92274 us
vector foreach time 6220 us
list foreach time 8857 us

test: times(10000000)
vector push_back time 1439122 us
vector emplace_back time 1423560 us
list push_back time 866928 us
list emplace_back time 889415 us
vector foreach time 62383 us
list foreach time 75673 us

  1 #include <iostream>
2 #include <vector>
3 #include <list>
4 #include <string>
5 #include <ctime>
6 #include <functional>
7 #include <sys/time.h>
8
9 using namespace std;
10
11 class Message
12 {
13 public:
14 std::string m1;
15 std::string m2;
16 std::string m3;
17 };
18
19 int test(long times)
20 {
21 std::cout << "test: times(" << times << ")" << std::endl;
22 vector<std::function<void(int, int)>> vt, vt2;
23 list<std::function<void(int, int)>> lt, lt2;
24
25 auto msg_o = [](int, int)
26 {
27 // std::cout << 111 << std::endl;
28 return 0;
29 };
30 std::function<void(int, int)> msg = msg_o;
31
32 // ******************************* vector push_back ************************
33 timeval tv_start;
34 gettimeofday(&tv_start, NULL);
35 for (long i = 0; i < times; ++i) {
36 vt.push_back(msg);
37 }
38
39 timeval tv_end;
40 gettimeofday(&tv_end, NULL);
41 std::cout << "vector push_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
42
43 // ******************************* vector emplace_back ************************
44 gettimeofday(&tv_start, NULL);
45 for (long i = 0; i < times; ++i) {
46 vt2.emplace_back(msg);
47 }
48
49 gettimeofday(&tv_end, NULL);
50 cout << "vector emplace_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
51
52
53 // ******************************* list push_back ************************
54 gettimeofday(&tv_start, NULL);
55 for (long i = 0; i < times; ++i) {
56 lt.push_back(msg);
57 }
58
59 gettimeofday(&tv_end, NULL);
60 cout << "list push_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
61
62 // ******************************* list emplace_back ************************
63 gettimeofday(&tv_start, NULL);
64 for (long i = 0; i < times; ++i) {
65 lt2.emplace_back(msg);
66 }
67
68 gettimeofday(&tv_end, NULL);
69 cout << "list emplace_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
70 // delete msg;
71 // msg = NULL;
72 //
73
74 // ******************************* vector foreach ************************
75 gettimeofday(&tv_start, NULL);
76 for (auto& elem : vt);
77 // elem(1, 1);
78
79 gettimeofday(&tv_end, NULL);
80 cout << "vector foreach time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
81
82
83 // ******************************* list foreach ************************
84 gettimeofday(&tv_start, NULL);
85 for (auto& elem : lt);
86 // elem(1, 1);
87
88 gettimeofday(&tv_end, NULL);
89 cout << "list foreach time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
90
91 std::cout << std::endl;
92 return 0;
93 }
94
95 int main()
96 {
97 test(1000);
98 test(10000);
99 test(100000);
100 test(1000000);
101 test(10000000);
102 return 0;
103 }

测试过象为lamda函数时:

test: times(1000)
vector push_back time 662 us
vector emplace_back time 427 us
list push_back time 506 us
list emplace_back time 387 us
vector foreach time 30 us
list foreach time 23 us

test: times(10000)
vector push_back time 1762 us
vector emplace_back time 1337 us
list push_back time 1197 us
list emplace_back time 1068 us
vector foreach time 80 us
list foreach time 59 us

test: times(100000)
vector push_back time 16146 us
vector emplace_back time 14225 us
list push_back time 12449 us
list emplace_back time 10368 us
vector foreach time 682 us
list foreach time 1809 us

test: times(1000000)
vector push_back time 147707 us
vector emplace_back time 108870 us
list push_back time 125867 us
list emplace_back time 89914 us
vector foreach time 6464 us
list foreach time 9816 us

test: times(10000000)
vector push_back time 1849568 us
vector emplace_back time 1419615 us
list push_back time 1228155 us
list emplace_back time 867557 us
vector foreach time 64897 us
list foreach time 73649 us

  1 #include <iostream>
2 #include <vector>
3 #include <list>
4 #include <string>
5 #include <ctime>
6 #include <functional>
7 #include <sys/time.h>
8
9 using namespace std;
10
11 class Message
12 {
13 public:
14 std::string m1;
15 std::string m2;
16 std::string m3;
17 };
18
19 int test(long times)
20 {
21 std::cout << "test: times(" << times << ")" << std::endl;
22 vector<std::function<void(int, int)>> vt, vt2;
23 list<std::function<void(int, int)>> lt, lt2;
24
25 auto msg = [](int, int)
26 {
27 // std::cout << 111 << std::endl;
28 return 0;
29 };
30 // std::function<void(int, int)> msg = msg_o;
31
32 // ******************************* vector push_back ************************
33 timeval tv_start;
34 gettimeofday(&tv_start, NULL);
35 for (long i = 0; i < times; ++i) {
36 vt.push_back(msg);
37 }
38
39 timeval tv_end;
40 gettimeofday(&tv_end, NULL);
41 std::cout << "vector push_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
42
43 // ******************************* vector emplace_back ************************
44 gettimeofday(&tv_start, NULL);
45 for (long i = 0; i < times; ++i) {
46 vt2.emplace_back(msg);
47 }
48
49 gettimeofday(&tv_end, NULL);
50 cout << "vector emplace_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
51
52
53 // ******************************* list push_back ************************
54 gettimeofday(&tv_start, NULL);
55 for (long i = 0; i < times; ++i) {
56 lt.push_back(msg);
57 }
58
59 gettimeofday(&tv_end, NULL);
60 cout << "list push_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
61
62 // ******************************* list emplace_back ************************
63 gettimeofday(&tv_start, NULL);
64 for (long i = 0; i < times; ++i) {
65 lt2.emplace_back(msg);
66 }
67
68 gettimeofday(&tv_end, NULL);
69 cout << "list emplace_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
70 // delete msg;
71 // msg = NULL;
72 //
73
74 // ******************************* vector foreach ************************
75 gettimeofday(&tv_start, NULL);
76 for (auto& elem : vt);
77 // elem(1, 1);
78
79 gettimeofday(&tv_end, NULL);
80 cout << "vector foreach time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
81
82
83 // ******************************* list foreach ************************
84 gettimeofday(&tv_start, NULL);
85 for (auto& elem : lt);
86 // elem(1, 1);
87
88 gettimeofday(&tv_end, NULL);
89 cout << "list foreach time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
90
91 std::cout << std::endl;
92 return 0;
93 }
94
95 int main()
96 {
97 test(1000);
98 test(10000);
99 test(100000);
100 test(1000000);
101 test(10000000);
102 return 0;
103 }

第三种测试,对move等的测试:

test: times(1000)
vector push_back time 292 us
vector emplace_back time 255 us
vector emplace_back time with move 400 us
list push_back time 262 us
list emplace_back time 342 us
list emplace_back time with move453 us
vector foreach time 29 us
list foreach time 25 us

test: times(10000)
vector push_back time 777 us
vector emplace_back time 843 us
vector emplace_back time with move 1105 us
list push_back time 732 us
list emplace_back time 788 us
list emplace_back time with move1055 us
vector foreach time 79 us
list foreach time 59 us

test: times(100000)
vector push_back time 9824 us
vector emplace_back time 7364 us
vector emplace_back time with move 9633 us
list push_back time 10814 us
list emplace_back time 7904 us
list emplace_back time with move10211 us
vector foreach time 689 us
list foreach time 761 us

test: times(1000000)
vector push_back time 64816 us
vector emplace_back time 70500 us
vector emplace_back time with move 95833 us
list push_back time 79398 us
list emplace_back time 81551 us
list emplace_back time with move107440 us
vector foreach time 6447 us
list foreach time 9765 us

test: times(10000000)
vector push_back time 850257 us
vector emplace_back time 858815 us
vector emplace_back time with move 1109211 us
list push_back time 760753 us
list emplace_back time 781865 us
list emplace_back time with move1030427 us
vector foreach time 67592 us
list foreach time 79275 us

  1 #include <iostream>
2 #include <vector>
3 #include <list>
4 #include <string>
5 #include <ctime>
6 #include <functional>
7 #include <sys/time.h>
8
9 using namespace std;
10
11 class Message
12 {
13 public:
14 std::string m1;
15 std::string m2;
16 std::string m3;
17 };
18
19 int test(long times)
20 {
21 std::cout << "test: times(" << times << ")" << std::endl;
22 vector<std::function<void(int, int)>> vt, vt2;
23 list<std::function<void(int, int)>> lt, lt2;
24
25 auto msg_o = [](int, int)
26 {
27 // std::cout << 111 << std::endl;
28 return 0;
29 };
30 std::function<void(int, int)> *msg = new std::function<void(int, int)>[times];
31 for (long i = 0; i < times; ++i)
32 {
33 msg[times] = msg_o;
34 }
35
36 // ******************************* vector push_back ************************
37 timeval tv_start;
38 gettimeofday(&tv_start, NULL);
39 for (long i = 0; i < times; ++i) {
40 vt.push_back(msg[i]);
41 }
42
43 timeval tv_end;
44 gettimeofday(&tv_end, NULL);
45 std::cout << "vector push_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
46
47 // ******************************* vector emplace_back ************************
48 gettimeofday(&tv_start, NULL);
49 for (long i = 0; i < times; ++i) {
50 vt2.emplace_back(msg[i]);
51 }
52
53 gettimeofday(&tv_end, NULL);
54 cout << "vector emplace_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
55
56 // ******************************* vector emplace_back with move ************************
57 gettimeofday(&tv_start, NULL);
58 for (long i = 0; i < times; ++i) {
59 vt2.emplace_back(std::move(msg[i]));
60 }
61
62 gettimeofday(&tv_end, NULL);
63 cout << "vector emplace_back time with move " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
64
65
66 // ******************************* list push_back ************************
67 gettimeofday(&tv_start, NULL);
68 for (long i = 0; i < times; ++i) {
69 lt.push_back(msg[i]);
70 }
71
72 gettimeofday(&tv_end, NULL);
73 cout << "list push_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
74
75 // ******************************* list emplace_back ************************
76 gettimeofday(&tv_start, NULL);
77 for (long i = 0; i < times; ++i) {
78 lt2.emplace_back(msg[i]);
79 }
80
81 gettimeofday(&tv_end, NULL);
82 cout << "list emplace_back time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
83
84 // ******************************* list emplace_back with move ************************
85 gettimeofday(&tv_start, NULL);
86 for (long i = 0; i < times; ++i) {
87 lt2.emplace_back(std::move(msg[i]));
88 }
89
90 gettimeofday(&tv_end, NULL);
91 cout << "list emplace_back time with move" << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
92 // delete msg;
93 // msg = NULL;
94 //
95
96 // ******************************* vector foreach ************************
97 gettimeofday(&tv_start, NULL);
98 for (auto& elem : vt);
99 // elem(1, 1);
100
101 gettimeofday(&tv_end, NULL);
102 cout << "vector foreach time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
103
104
105 // ******************************* list foreach ************************
106 gettimeofday(&tv_start, NULL);
107 for (auto& elem : lt);
108 // elem(1, 1);
109
110 gettimeofday(&tv_end, NULL);
111 cout << "list foreach time " << (tv_end.tv_sec - tv_start.tv_sec) * 1000000 + tv_end.tv_usec - tv_start.tv_usec << " us" << std::endl;
112
113 std::cout << std::endl;
114 return 0;
115 }
116
117 int main()
118 {
119 test(1000);
120 test(10000);
121 test(100000);
122 test(1000000);
123 test(10000000);
124 return 0;
125 }

std::vector与std::list效能对比(基于c++11)的更多相关文章

  1. 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)

    string.vector 互转 string 转 vector vector  vcBuf;string        stBuf("Hello DaMao!!!");----- ...

  2. 【C++11应用】基于C++11及std::thread实现的线程池

    目录 基于C++11及std::thread实现的线程池 基于C++11及std::thread实现的线程池 线程池源码: #pragma once #include <functional&g ...

  3. 编程杂谈——std::vector与List<T>的性能比较

    昨天在比较完C++中std::vector的两个方法的性能差异并留下记录后--编程杂谈--使用emplace_back取代push_back,今日尝试在C#中测试对应功能的性能. C#中对应std:: ...

  4. Item 21: 比起直接使用new优先使用std::make_unique和std::make_shared

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 让我们先从std::make_unique和std::make_s ...

  5. 使用std::map和std::list存放数据,消耗内存比实际数据大得多

    使用std::map和std::list存放数据,消耗内存比实际数据大得多 场景:项目中需要存储一个结构,如下程序段中TEST_DATA_STRU,结构占24B.但是使用代码中的std::list&l ...

  6. std::array,std::vector,基于范围的for循环

    std::array除了有传统数组支持随机访问.效率高.存储大小固定等特点外,还支持迭代器访问.获取容量.获得原始指针等高级功能.而且它还不会退化成指针T *给开发人员造成困惑. for( 元素名变量 ...

  7. C++基于范围的for循环性能测试(针对std::vector)

    1.代码如下: void output1(int x){ if (x == 10000000) { std::cout << x << std::endl; } }const ...

  8. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  9. std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义

    std::vector<Channel2*> m_allChannels;容器,以及如何根据channelid的意义 这个容器保存了所有客户端连接的channel Channel2* Li ...

随机推荐

  1. 前台console调试技巧

    前台console调试技巧 一.console.log() 二.console.warn() 三.console.dir() 四.console.table() 五.console.assert() ...

  2. Spring Boot 两种多数据源配置:JdbcTemplate、Spring-data-jpa

    多数据源配置 JdbcTemplate支持 Spring-data-jpa支持 多数据源配置 创建一个Spring配置类,定义两个DataSource用来读取application.propertie ...

  3. 利用selenium抓取网页的ajax请求

    部门需要一个自动化脚本,完成web端界面功能的冒烟,并且需要抓取加载页面时的ajax请求,从接口层面判断请求是否成功.查阅了很多资料都没有人有过相关问题的处理经验,在处理过程中也踩了很多坑,所以如果你 ...

  4. 在线工具生成接入信息mqtt.fx快速接入阿里云

    在线工具生成接入信息mqtt.fx快速接入阿里云 在使用阿里云获取的三元组信息进行接入的时候,往往需要加密生成接入信息之后才能进行接入,因此我根据阿里云提供的加密工具实现了一个阿里云物联网平台mqtt ...

  5. Redis命令之setbit

    setbit的作用是,对key上存储的字符串,设置或清除指定偏移量上的位(bit). 语法如下: SETBIT key offset value key是要操作的对象的键. offset是操作对象上的 ...

  6. H - 看病要排队

    看病要排队这个是地球人都知道的常识.不过经过细心的0068的观察,他发现了医院里排队还是有讲究的.0068所去的医院有三个医生(汗,这么少)同时看病.而看病的人病情有轻重,所以不能根据简单的先来先服务 ...

  7. 2019牛客暑期多校训练营(第一场)E ABBA (DP/卡特兰数)

    传送门 知识点:卡特兰数/动态规划 法一:动态规划 由题意易知字符串的任何一个前缀都满足\(cnt(A) - cnt(B) \le n , cnt(B)-cnt(A)\le m\) \(d[i][j] ...

  8. 【noi 2.6_687】Color Tunnels(DP)

    P.S.o(︶︿︶)o 唉~虽然这题方程不难,但题目长,代码长,我花了超过3小时!(>﹏<)悲伤辣么大~~~ 谨此题解惠及众人,hh. 题意:给定长度为M的一串颜色序列,和平面上的N个颜色 ...

  9. 哈希算法解决:HDU1686 && POJ2774 && POJ3261

    HDU1686 题意: 找A串在B串中的出现次数(可重叠),可用KMP做,这里只提供哈希算法做的方法 题解: 先得到A串的hash值,然后在B中枚举起点,长度为lena的子串,检验hash值是否相同就 ...

  10. servlet接口实现类HttpServlet以及开发中一些细节

    1. 但是eclipse不会帮我们改web.xml配置文件,所以我们也要在web.xml文件里面手动改 2. 这个样子的话你在用浏览器访问的时候链接的映射就改成了t_day05,这个主要用于你建立完一 ...