-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (143 loc) · 5.3 KB
/
main.cpp
File metadata and controls
174 lines (143 loc) · 5.3 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
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
#include <iostream>
#include <map>
#include <chrono>
#include <vector>
#include <cstring>
struct MeasureScope {
MeasureScope(const std::string& name) :
name(name),
start(std::chrono::high_resolution_clock::now())
{
}
~MeasureScope()
{
auto end = std::chrono::high_resolution_clock::now();
std::cout << name << " took \t" << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() << "ns\n";
}
std::string name;
std::chrono::time_point<std::chrono::high_resolution_clock> start;
};
#define NUM_ELEMENTS (10*1000*1000)
typedef int64_t T;
static T data[NUM_ELEMENTS];
void generateRandomData(T *data, size_t num_elements)
{
for (int i = 0; i < num_elements; i ++){
data[i] = rand();
//data[i] = NUM_ELEMENTS - i;
}
}
template <typename T>void test_map(const T *data, size_t num_elements, size_t threshold, uint64_t *results)
{
std::multimap<T, size_t> mapItems;
for ( size_t i = 0; i < num_elements; i ++)
{
mapItems.insert({data[i], i});
while (mapItems.size() > threshold)
mapItems.erase(--mapItems.end());
}
size_t i = 0;
for (auto items: mapItems){
results[i++] = items.second;
}
}
template <typename T> struct SortableItem
{
uint64_t a;
T b;
bool operator < (const SortableItem& other) const {return (this->a < other.a);}
};
void test_sort(const T *data, size_t num_elements, size_t threshold, uint64_t *results)
{
threshold = std::min(threshold, num_elements);
SortableItem <T>*dataIndexed = new SortableItem<T>[num_elements];
for (size_t i = 0; i < num_elements; i ++)
{
dataIndexed[i].a = data[i];
dataIndexed[i].b = i;
}
std::sort(dataIndexed, dataIndexed + num_elements);
for (size_t i = 0; i < threshold; i ++){
results[i] = dataIndexed[i].b;
}
delete []dataIndexed;
}
void test_custom1(const T *data, size_t num_elements, size_t threshold, uint64_t *results)
{
threshold = std::min(num_elements, threshold);
int current_max = 0;
int cur;
int z;
for ( size_t i = 0; i < num_elements; i ++) {
//We starting from the highest values and we look for the immediately lower than the given one
for (cur = current_max; cur > 0 && (data[i] < data[results[cur - 1]]); cur--);
if (cur < threshold) {
//Move all the higher values 1 position to the right
for (z = current_max -1; z >= cur; z--)
results[z + 1] = results[z];
current_max = std::min((int)threshold, current_max+1);
//insert element into the given position
results[cur] = i;
}
}
}
template <typename T> void GetFirstElements(T *data, size_t num_elements, size_t threshold, uint64_t *results)
{
threshold = std::min(threshold, num_elements);
SortableItem<T> *dataIndexed = new SortableItem<T>[num_elements];
for (size_t i = 0; i < num_elements; i++) {
dataIndexed[i].a = data[i];
dataIndexed[i].b = i;
}
std::nth_element(dataIndexed, dataIndexed + threshold, dataIndexed + num_elements);
std::sort(dataIndexed, dataIndexed + threshold);
for (size_t i = 0; i < threshold; i++) {
results[i] = dataIndexed[i].b;
}
delete []dataIndexed;
}
void test_nth(T *data, size_t num_elements, size_t threshold, uint64_t *results) {
GetFirstElements(data, num_elements, threshold, results);
}
int main(int argc, char *argv[]) {
std::cout << "Parameters " << argc << std::endl;
std::cout << "Generating random data...";
generateRandomData(data, NUM_ELEMENTS);
std::cout << "done" << std::endl;
size_t threshold = 10;
uint64_t results[threshold];
std::cout << "Getting first " << threshold << " elements ..." << std::endl;
for (size_t i = 0; i < threshold; i ++) {results[i] = 0;}
{
MeasureScope measure("test_map");
test_map(data, NUM_ELEMENTS, threshold, results);
std::cout << "done" << std::endl;
for (size_t i = 0; i < std::min(threshold,(size_t)5); i ++) { std::cout << data[results[i]] << " "; }
}
std::memset(results, 0, sizeof(results));
for (size_t i = 0; i < threshold; i ++) {results[i] = 0;}
{
MeasureScope measure("test_sort");
test_sort(data, NUM_ELEMENTS, threshold, results);
std::cout << "done" << std::endl;
for (size_t i = 0; i < std::min(threshold,(size_t)5); i ++) { std::cout << data[results[i]] << " "; }
}
std::memset(results, 0xFF, sizeof(results));
for (size_t i = 0; i < threshold; i ++) {results[i] = 0;}
{
MeasureScope measure("test_custom1");
test_custom1(data, NUM_ELEMENTS, threshold, results);
std::cout << "done" << std::endl;
for (size_t i = 0; i < std::min(threshold,(size_t)5); i ++) { std::cout << data[results[i]] << " "; }
}
std::memset(results, 0, sizeof(results));
for (size_t i = 0; i < threshold; i ++) {results[i] = 0;}
{
MeasureScope measure("test_nth");
test_nth(data, NUM_ELEMENTS, threshold, results);
std::cout << "done" << std::endl;
for (size_t i = 0; i < std::min(threshold,(size_t)5); i ++) { std::cout << data[results[i]] << " "; }
}
std::cout << std::endl;
return 0;
}