【发布时间】:2011-10-08 02:48:01
【问题描述】:
我正在比较我编写的一个简单的哈希函数,它只是将它乘以一个素数 mod 另一个素数(表大小),结果 stl 慢了 100 倍。这是我写的测试方法:
stdext:: hash_map<string, int> hashDict;
for (int l = 0; l < size; ++l){
hashDict[arr[l]] = l;
}
long int before3 = GetTickCount();
int c = 0;
while (c < size){
hashDict[arr[c]];
c++;
}
long int after3 = GetTickCount();
cout << "for stl class, the time is " << (after3 - before3) / 1000.0 << '\n';
cout << "the average is " << ((after3 - before3) / 1000.0 ) /long (size) << '\n';
字典的大小约为 200k 个元素,而我写的 hash 函数的表大小有 3m 个条目,所以可能与 stl 类的表大小非常小有关。有谁知道stl函数的tablesize和collision rates.etc?
【问题讨论】:
-
这是我的哈希函数: unsigned hash(const char *s) { unsigned hashval; for (hashval = 0; *s != '\0'; s++) hashval = *s + PRIME * hashval;返回哈希值 % HASHSIZE; }
-
这真的,真的需要更多信息来回答。你的简单哈希函数是什么,它有什么作用?另外,你在说什么
hash_map实现? STL 中没有一个(会有一个std::unordered_map<>)。最后,您没有对提供的代码中的哈希做任何事情;你确定你的版本在做什么? -
我只是从地图中检索元素,没有对它做任何事情。
-
@SuperString:你确定你真的在检索它们吗?如果你没有对它们做任何事情,你怎么知道你所做的一切都没有被优化?每当有一百倍的差异时,我怀疑编译器优化了一些东西。
标签: c++ hash c++builder