-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_performance.cpp
More file actions
43 lines (35 loc) · 1.05 KB
/
Copy pathvector_performance.cpp
File metadata and controls
43 lines (35 loc) · 1.05 KB
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
#include <iostream>
#include <vector>
using namespace std;
//Tests the time of the vector "pop_back()" operation versus the vector "erase" operation
int main(){
int num = 100000;
vector<int> vect;
vector<int> vect2;
vect.reserve(num);
vect2.reserve(num);
for (int i = 0; i < num; i++){
vect.push_back(i);
}
for (int i = 0; i < num; i++){
vect2.push_back(i);
}
clock_t begin = clock();
for (int i = 0; i < num; i++){
vect.erase(vect.begin()+0);
}
clock_t end = clock();
double elapsed_secs = double(end - begin) /CLOCKS_PER_SEC;
cout << fixed << endl;
cout << "popzero = " << elapsed_secs << endl;
clock_t begin2 = clock();
for (int i = 0; i < num; i++){
vect2.pop_back();
}
clock_t end2 = clock();
double elapsed_secs2 = double(end2 - begin2) /CLOCKS_PER_SEC;
cout << fixed << endl;
cout << "popend = " << elapsed_secs2 << endl;
cout << "\nPopping from the end is " << elapsed_secs/elapsed_secs2 <<" times faster." << endl;
return 0;
}