-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
71 lines (62 loc) · 2.48 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
#include <vector>
#include <numeric>
#include <string>
#include <algorithm>
#include <iostream>
#include <cassert>
#include "ZipIterator.hpp"
#include <chrono>
template<typename Clock>
static inline double timediff2ms(std::chrono::time_point<Clock> start, std::chrono::time_point<Clock> stop) {
if(stop < start) std::swap(stop, start);
return std::chrono::duration<double, std::milli>(stop - start).count();
}
int main(int argc, char **argv) {
size_t nelem = 10000;
if(argc > 1) {
nelem = std::strtoull(argv[1], nullptr, 10);
}
std::vector<int> a{3,1,4,2};
std::vector<std::string> b{"Alice","Bob","Charles","David"};
auto zip = Zip(a,b);
for (const auto & z: zip) std::cout << z << std::endl;
std::cout << std::endl;
std::sort(zip.begin(), zip.end());
for (const auto & z: zip) std::cout << z << std::endl;
std::vector<int> ai(nelem);
std::iota(ai.begin(), ai.end(), 0);
std::vector<std::string> bi(nelem);
for(auto &i: bi) i = std::to_string(&i - bi.data());
for(const auto &[x, y]: Zip(ai, bi)) {
assert(std::to_string(x) == y);
}
#if 1
for(const auto &z: Zip(ai, bi)) {
auto &[x,y] = z;
assert(std::to_string(x) == y);
}
#endif
Zip zi(ai, bi);
auto start = std::chrono::high_resolution_clock::now();
std::sort(zi.begin(), zi.end(), [](const auto &x, const auto &y) {
return std::get<1>(x) < std::get<1>(y);
});
auto stop = std::chrono::high_resolution_clock::now();
assert(std::is_sorted(bi.begin(), bi.end()));
assert(!std::is_sorted(ai.begin(), ai.end()));
std::iota(ai.begin(), ai.end(), 0);
for(auto &i: bi) i = std::to_string(&i - bi.data());
std::fprintf(stderr, "Took %gms to sort iterator pairs\n", timediff2ms(start, stop));
const Zip zi2(ai, bi);
std::vector<std::pair<int, std::string>> vals;
std::for_each(zi2.begin(), zi2.end(), [&](const auto &xy) {
auto &x = std::get<0>(xy);
auto &y = std::get<1>(xy);
vals.emplace_back(x, y);
});
start = std::chrono::high_resolution_clock::now();
std::sort(vals.begin(), vals.end(), [](const auto &x, const auto &y) {return x.second < y.second;});
stop = std::chrono::high_resolution_clock::now();
std::fprintf(stderr, "Took %gms to sort pairs\n", timediff2ms(start, stop));
return 0;
}