-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
184 lines (148 loc) · 4.08 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <map>
#include <random>
#include <chrono>
#include <algorithm>
#include "array_map.h"
inline long long Rand() noexcept {
return rand(); // (rand() & 255) + ((rand() & 255) << 8) + ((rand() & 255) << 16) + ((rand() & 255) << 24);
}
int main(void)
{
try {
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::vector<int> x(10240000, 0);
//std::sort(x.begin(), x.end());
long long max = 0;
for (int i = 0; i < x.size(); ++i) {
x[i] = i;
if (x[i] > max) {
max = x[i];
}
}
std::default_random_engine rng(seed);
std::shuffle(x.begin(), x.end(), rng);
int a = clock();
wiz::array_map<int, int> map;
map.reserve2(x.size());
for (int i = 0; i < x.size(); ++i) {
map.lazy_insert(std::make_pair(x[i], x[i] + 1));
//map[x[i]];
}
map[max] = 0;
int b = clock();
//map.PrintTree();
std::cout << "array_map random_insert ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
a = clock();
std::map<int, int> map2;
for (int i = 0; i < x.size(); ++i) {
map2.insert(std::make_pair(x[i], x[i] + 1));
}
b = clock();
//map2[max]= 0;
std::cout << "std::map random_insert ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
{
a = clock();
auto map2 = std::map<int, int>();
auto pairs = std::vector<std::pair<int, int>>();
pairs.reserve(x.size());
for (int i = 0; i < x.size(); ++i) {
pairs.push_back({ x[i] , x[i] + 1 });
}
std::stable_sort(std::execution::par, pairs.begin(), pairs.end());
int c = clock();
map2.insert(pairs.begin(), pairs.end());
b = clock();
std::cout << "std::map insert2 ";
std::cout << b - a << "ms\t" << b-c << "ms\t" << c-a <<"ms\n";
std::cout << map2[max] << " ";
std::cout << "------------------------\n";
a = clock();
for (int i = 0; i < x.size(); ++i) {
if (map2.end() == map2.find(x[i])) {
return -2;
}
}
b = clock();
std::cout << "std::map random_search? ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
}
std::shuffle(x.begin(), x.end(), std::default_random_engine(seed));
a = clock();
for (int i = 0; i < x.size(); ++i) {
if (map.end() == map.find(x[i])) {
return -1;
}
}
b = clock();
std::cout << "array_map random_search ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
a = clock();
for (int i = 0; i < x.size(); ++i) {
if (map2.end() == map2.find(x[i])) {
return -2;
}
}
b = clock();
std::cout << "std::map random_search ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
{
a = clock();
auto map1 = wiz::array_map<int, int>();
for (int i = 0; i < x.size(); ++i) {
map1.insert(std::make_pair(i, i));
}
map1[max] = 0;
b = clock();
std::cout << "array_map insert-sorted_order";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
a = clock();
for (int i = 0; i < x.size(); ++i) {
if (map1.end() == map1.find(x[i])) {
return -1;
}
}
b = clock();
std::cout << "array_map random_search ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
}
{
a = clock();
auto map2 = std::map<int, int>();
for (int i = 0; i < x.size(); ++i) {
map2.insert(std::make_pair(i, i));
}
b = clock();
std::cout << "std::map insert-sorted_order";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
a = clock();
for (int i = 0; i < x.size(); ++i) {
if (map2.end() == map2.find(x[i])) {
return -2;
}
}
b = clock();
std::cout << "std::map random_search ";
std::cout << b - a << "ms\n";
std::cout << "------------------------\n";
}
}
catch (const std::exception& e)
{
std::cout << e.what() << "\n";
}
return 0;
}