-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashHitOfUMP.cpp
39 lines (35 loc) · 1.19 KB
/
hashHitOfUMP.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
#include <ctime>
#include <iostream>
#include <unordered_map>
#include <chrono>
using namespace std;
const int N = 2e5;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void insert_numbers(long long x) {
clock_t begin = clock();
unordered_map<long long, int> numbers; //hash碰撞
// unordered_map<long long, int,custom_hash> numbers; //hash碰撞解决
for (int i = 1; i <= N; i++){
numbers[i * x] = i;
}
long long sum = 0;
for (auto &entry : numbers)
sum += (entry.first / x) * entry.second;
printf("x = %lld: %.3lf seconds, sum = %lld\n", x, (double) (clock() - begin) / CLOCKS_PER_SEC, sum);
}
int main() {
insert_numbers(107897);
insert_numbers(126271);//若不用 custom_hash会疯狂hash碰撞 导致速度极慢
}