-
Notifications
You must be signed in to change notification settings - Fork 0
/
meas.cpp
94 lines (73 loc) · 1.92 KB
/
meas.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
#include <iostream>
#include <fstream>
#include <cinttypes>
#include <vector>
#include <random>
#include <string>
#include <stdexcept>
#include "hashmap.hpp"
#include "helpers.hpp"
using namespace std;
inline uint64_t xor_hash(const string& str) {
uint64_t hash = 0;
size_t i = 0;
for (const auto& c: str) {
hash ^= static_cast<const uint64_t>(c) << 8 * i;
i = (i + 1) % 8;
}
return hash;
}
#ifdef ASMOPTIMIZATION
extern "C" uint64_t __xor_hash(const char* str, size_t len);
inline uint64_t xor_hash_asm(const string& str) {
return __xor_hash(str.data(), str.size());
}
#endif
class randdist {
private:
static random_device rd;
mt19937 gen;
uniform_int_distribution<> dis;
public:
randdist(int st, int ed) : dis(st, ed), gen(randdist::rd()) {}
int next() {
return dis(gen);
}
};
random_device randdist::rd;
#ifdef ASMOPTIMIZATION
extern "C" bool __strcmp_asm(const char* s1, const char* s2, size_t len);
struct streqasm {
bool operator()(const string& lhs, const string& rhs) const {
if (lhs.size() != rhs.size())
return false;
return __strcmp_asm(lhs.data(), rhs.data(), lhs.size());
}
};
#endif
random_device rd;
int main(int argc, char** argv) {
int amount = 10000;
if (argc >= 2) amount = stoi(argv[1]);
int multiply = 500;
if (argc == 3) multiply = stoi(argv[2]);
auto lines = load_lines("strings");
if (lines.size() == 0)
throw runtime_error("Empty strings file");
cout << "Lines loaded" << endl;
shuffle(lines.begin(), lines.end(), rd);
if (lines.size() > amount) lines.resize(amount);
cout << "Processing " << lines.size() << " lines "
<< multiply << " times" << endl;
randdist dist(0, lines.size() - 1);
#ifdef ASMOPTIMIZATION
HashMap<string, streqasm> hmap(xor_hash_asm);
#else
HashMap<string> hmap(xor_hash);
#endif
for (int i = 0; i < multiply * lines.size(); ++i) {
hmap.insert(lines[dist.next()]);
hmap.contains(lines[dist.next()]);
hmap.erase(lines[dist.next()]);
}
}