1
/*
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : VectorVsList.cpp
5Compiler : Visual C++ 8.0
6Description : Demo the performance difference between std::vector and std::list
7*/
8 #include < iostream >
9 #include < ctime >
10 #include < vector >
11 #include < list >
12
13 // Add to Vector at end
14 void addToVectorAtEnd();
15 // Add to List at end
16 void addToListAtEnd();
17 // Calculate time for adding to end
18 void addToEnd();
19
20 // Add to Vector anywhere
21 void addToVectorAnywhere();
22 // Add to List anywhere
23 void addToListAnywhere();
24 // Calculate time for adding anywhere
25 void addAnywhere();
26
27 // Remove from Vector anywhere
28 void removeFromVectorAnywhere();
29 // Remove from List anywhere
30 void removeFromListAnywhere();
31 // Calculate time for removing anywhere
32 void removeAnywhere();
33
34 // Non-sequential access to Vector
35 void nonSequentialAccessToVector();
36 // Non-sequential access to List
37 void nonSequentialAccessToList();
38 // Caculate time for non-sequential access to
39 void nonSequentialAccess();
40
41 std::vector < int > vector1;
42 std::list < int > list1;
43
44 int main() {
45 // Calculate time for adding to end
46 addToEnd();
47
48 // Calculate time for adding anywhere
49 addAnywhere();
50
51 // Calculate time for removing anywhere
52 removeAnywhere();
53
54 // Caculate time for non-sequential access to
55 nonSequentialAccess();
56}
57
58 // Add to Vector at end
59 void addToVectorAtEnd() {
60 for(int i=0; i != 1000000; ++i) {
61 vector1.push_back(i);
62 }
63}
64
65 // Add to List at end
66 void addToListAtEnd() {
67 for(int i=0; i != 1000000; ++i) {
68 list1.push_back(i);
69 }
70}
71
72 // Calculate time for adding to end
73 void addToEnd() {
74 clock_t addToVectorAtEndClock = clock();
75 addToVectorAtEnd();
76 addToVectorAtEndClock = clock() - addToVectorAtEndClock;
77
78 std::cout << "Vector Insertion at the end Process time:" << (double)addToVectorAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
79
80 clock_t addToListAtEndClock = clock();
81 addToListAtEnd();
82 addToListAtEndClock = clock() - addToListAtEndClock;
83
84 std::cout << "List Insertion at the end Process time:" << (double)addToListAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
85
86 if (addToVectorAtEndClock < addToListAtEndClock) {
87 std::cout << "Insertion at the end : Vector wins" << std::endl;
88 }
89 else {
90 std::cout << "Insertion at the end : List wins" << std::endl;
91 }
92}
93
94 // Add to Vector anywhere
95 void addToVectorAnywhere() {
96 // Add to 50000th
97 std::vector<int>::iterator iter = vector1.begin();
98
99 for(int i = 0; i <= 500; ++i) {
100 ++iter;
101 iter = vector1.insert(iter,i);
102 }
103}
104
105 // Add to List anywhere
106 void addToListAnywhere() {
107 // Add to 50000th
108 std::list<int>::iterator iter = list1.begin();
109
110 for(int i = 0; i != 500; ++i) {
111 ++iter;
112 iter = list1.insert(iter,i);
113 }
114}
115
116 // Calculate time for adding anywhere
117 void addAnywhere() {
118 clock_t addToVectorAnywhereClock = clock();
119 addToVectorAnywhere();
120 addToVectorAnywhereClock = clock() - addToVectorAnywhereClock;
121
122 std::cout << "Vector Insertion anywhere Process time:" << (double)addToVectorAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
123
124 clock_t addToListAnywhereClock = clock();
125 addToListAnywhere();
126 addToListAnywhereClock = clock() - addToListAnywhereClock;
127
128 std::cout << "List Insertion anywhere Process time:" << (double)addToListAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
129
130
2(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4Filename : VectorVsList.cpp
5Compiler : Visual C++ 8.0
6Description : Demo the performance difference between std::vector and std::list
7*/
8 #include < iostream >
9 #include < ctime >
10 #include < vector >
11 #include < list >
12
13 // Add to Vector at end
14 void addToVectorAtEnd();
15 // Add to List at end
16 void addToListAtEnd();
17 // Calculate time for adding to end
18 void addToEnd();
19
20 // Add to Vector anywhere
21 void addToVectorAnywhere();
22 // Add to List anywhere
23 void addToListAnywhere();
24 // Calculate time for adding anywhere
25 void addAnywhere();
26
27 // Remove from Vector anywhere
28 void removeFromVectorAnywhere();
29 // Remove from List anywhere
30 void removeFromListAnywhere();
31 // Calculate time for removing anywhere
32 void removeAnywhere();
33
34 // Non-sequential access to Vector
35 void nonSequentialAccessToVector();
36 // Non-sequential access to List
37 void nonSequentialAccessToList();
38 // Caculate time for non-sequential access to
39 void nonSequentialAccess();
40
41 std::vector < int > vector1;
42 std::list < int > list1;
43
44 int main() {
45 // Calculate time for adding to end
46 addToEnd();
47
48 // Calculate time for adding anywhere
49 addAnywhere();
50
51 // Calculate time for removing anywhere
52 removeAnywhere();
53
54 // Caculate time for non-sequential access to
55 nonSequentialAccess();
56}
57
58 // Add to Vector at end
59 void addToVectorAtEnd() {
60 for(int i=0; i != 1000000; ++i) {
61 vector1.push_back(i);
62 }
63}
64
65 // Add to List at end
66 void addToListAtEnd() {
67 for(int i=0; i != 1000000; ++i) {
68 list1.push_back(i);
69 }
70}
71
72 // Calculate time for adding to end
73 void addToEnd() {
74 clock_t addToVectorAtEndClock = clock();
75 addToVectorAtEnd();
76 addToVectorAtEndClock = clock() - addToVectorAtEndClock;
77
78 std::cout << "Vector Insertion at the end Process time:" << (double)addToVectorAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
79
80 clock_t addToListAtEndClock = clock();
81 addToListAtEnd();
82 addToListAtEndClock = clock() - addToListAtEndClock;
83
84 std::cout << "List Insertion at the end Process time:" << (double)addToListAtEndClock/CLOCKS_PER_SEC << " sec" << std::endl;
85
86 if (addToVectorAtEndClock < addToListAtEndClock) {
87 std::cout << "Insertion at the end : Vector wins" << std::endl;
88 }
89 else {
90 std::cout << "Insertion at the end : List wins" << std::endl;
91 }
92}
93
94 // Add to Vector anywhere
95 void addToVectorAnywhere() {
96 // Add to 50000th
97 std::vector<int>::iterator iter = vector1.begin();
98
99 for(int i = 0; i <= 500; ++i) {
100 ++iter;
101 iter = vector1.insert(iter,i);
102 }
103}
104
105 // Add to List anywhere
106 void addToListAnywhere() {
107 // Add to 50000th
108 std::list<int>::iterator iter = list1.begin();
109
110 for(int i = 0; i != 500; ++i) {
111 ++iter;
112 iter = list1.insert(iter,i);
113 }
114}
115
116 // Calculate time for adding anywhere
117 void addAnywhere() {
118 clock_t addToVectorAnywhereClock = clock();
119 addToVectorAnywhere();
120 addToVectorAnywhereClock = clock() - addToVectorAnywhereClock;
121
122 std::cout << "Vector Insertion anywhere Process time:" << (double)addToVectorAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
123
124 clock_t addToListAnywhereClock = clock();
125 addToListAnywhere();
126 addToListAnywhereClock = clock() - addToListAnywhereClock;
127
128 std::cout << "List Insertion anywhere Process time:" << (double)addToListAnywhereClock/CLOCKS_PER_SEC << " sec" << std::endl;
129
130