-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
82 lines (68 loc) · 2.64 KB
/
main.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
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
#include "map.h"
#include "timer.h"
#include <map>
#define INSERT_ELEMENTS
#define SEARCH_ELEMENTS
#define ERASE_ELEMENTS
#define RANDOM_OPERATIONS
int main() {
map<int> myMap;
std::map<int, int> map;
Timer timer;
int range = 10000;
int numInsert = 1000;
int numSearch = 1000;
int numDelete = 1000;
int numOps = 1000;
#if defined INSERT_ELEMENTS
timer.restart();
for (int i = 0; i < numInsert; i++)
map.insert(std::pair<int, int>(rand() % range, rand() % range));
printf("Insert %u elements in a std::map = %.3f milliseconds\n", numInsert, timer.elapsed<float>() * 1000);
timer.restart();
for (int i = 0; i < numInsert; i++)
myMap.insert(rand() % range, rand() % range);
printf("Insert %u elements in my map = %.3f milliseconds\n\n", numInsert, timer.elapsed<float>() * 1000);
#endif
#if defined SEARCH_ELEMENTS
timer.restart();
for (int i = 0; i < numSearch; i++)
map.find(rand() % range);
printf("Search %u elements in std::map = %.3f milliseconds\n", numSearch, timer.elapsed<float>() * 1000);
timer.restart();
for (int i = 0; i < numSearch; i++)
myMap.find(rand() % range);
printf("Search %u elements in my map = %.3f milliseconds\n\n", numSearch, timer.elapsed<float>() * 1000);
#endif
#if defined ERASE_ELEMENTS
timer.restart();
for (int i = 0; i < numDelete; i++)
map.erase(rand() % range);
printf("Delete %u elements in a std::map = %.3f milliseconds\n", numDelete, timer.elapsed<float>() * 1000);
timer.restart();
for (int i = 0; i < numDelete; i++)
myMap.erase(rand() % range);
printf("Delete %u elements in my map = %.3f milliseconds\n\n", numDelete, timer.elapsed<float>() * 1000);
#endif
#if defined RANDOM_OPERATIONS
timer.restart();
for (int i = 0; i < numOps; ++i) {
switch (rand() % 3) {
case 0: map.insert(std::pair<int, int>(rand() % range, rand() % range)); break;
case 1: map.erase(rand() % range); break;
case 2: map.find(rand() % range); break;
}
}
printf("Random sequence of %u operations with std::map = %.3f milliseconds\n", numOps, timer.elapsed<float>() * 1000);
timer.restart();
for (int i = 0; i < numOps; ++i) {
switch (rand() % 3) {
case 0: myMap.insert(rand() % range, rand() % range); break;
case 1: myMap.erase(rand() % range); break;
case 2: myMap.find(rand() % range); break;
}
}
printf("Random sequence of %u operations with my map = %.3f milliseconds\n", numOps, timer.elapsed<float>() * 1000);
#endif
return 0;
}