-
Notifications
You must be signed in to change notification settings - Fork 0
/
traversal-vector.cpp
39 lines (37 loc) · 970 Bytes
/
traversal-vector.cpp
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
#include <sys/time.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<int> v;
for (int i = 1; i <= 1000000; i++) {
v.push_back(i);
}
struct timeval tv;
gettimeofday(&tv, NULL);
cout << "time is " << tv.tv_sec << ":" << tv.tv_usec << endl;
size_t size = v.size();
int a = 0;
for (size_t i = 0; i < size; i++) {
a = v[i];
}
cout << "a:" << a << endl;
gettimeofday(&tv, NULL);
cout << "time is " << tv.tv_sec << ":" << tv.tv_usec << endl;
a = 0;
for (vector<int>::iterator iter = v.begin(); iter != v.end(); iter++) {
a = *iter;
}
cout << "a:" << a << endl;
gettimeofday(&tv, NULL);
cout << "time is " << tv.tv_sec << ":" << tv.tv_usec << endl;
a = 0;
for (auto iter = v.cbegin(); iter != v.cend(); iter++) {
a = *iter;
}
cout << "a:" << a << endl;
gettimeofday(&tv, NULL);
cout << "time is " << tv.tv_sec << ":" << tv.tv_usec << endl;
return 0;
}