下面的代码在不同的硬件平台上运行时得到了不同的输出顺序。
#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>
#include <functional>
#include <string>
struct hash_func {
std::size_t operator()(const std::string &key) const {
std::size_t hash = 0U;
const std::size_t mask = 0xF0000000;
for (std::string::size_type i = 0; i < key.length(); ++i) {
hash = (hash << 4U) + key[i];
std::size_t x = hash & mask;
if (x != 0)
hash ^= (x >> 24);
hash &= ~x;
}
std::cout << "for key " << key << " hash " << hash << std::endl;
return hash;
}
};
void show() {
std::vector<std::string> v;
v.push_back("n1YxyaBzoRogNh72eri7HBGijCAtcHpf9nm,");
v.push_back("n1GV9UScgncwU6KKL9T18mCo2S6uAE69SWs,");
v.push_back("n1X2SXyEKej7GZgAreXDCkiT59qaYKBDcYi,");
std::unordered_set<std::string, hash_func> s;
for (auto it = v.begin(); it != v.end(); it++) {
s.insert(*it);
}
for (auto it = s.begin(); it != s.end(); it++) {
std::cout << *it << std::endl;
}
}
int main() {
show();
return 0;
}
结果是:
1) Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
for key n1YxyaBzoRogNh72eri7HBGijCAtcHpf9nm, hash 43411516
for key n1GV9UScgncwU6KKL9T18mCo2S6uAE69SWs, hash 93983804
for key n1X2SXyEKej7GZgAreXDCkiT59qaYKBDcYi, hash 17984604
n1X2SXyEKej7GZgAreXDCkiT59qaYKBDcYi,
n1GV9UScgncwU6KKL9T18mCo2S6uAE69SWs,
n1YxyaBzoRogNh72eri7HBGijCAtcHpf9nm,
2) AMD EPYC 7501 32 核处理器
for key n1YxyaBzoRogNh72eri7HBGijCAtcHpf9nm, hash 43411516
for key n1GV9UScgncwU6KKL9T18mCo2S6uAE69SWs, hash 93983804
for key n1X2SXyEKej7GZgAreXDCkiT59qaYKBDcYi, hash 17984604
n1X2SXyEKej7GZgAreXDCkiT59qaYKBDcYi,
n1YxyaBzoRogNh72eri7HBGijCAtcHpf9nm,
n1GV9UScgncwU6KKL9T18mCo2S6uAE69SWs,