- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在类的初始化中实现了一个具有可变大小存储桶的哈希表,只是一个在运行时调整大小的链表数组。
问题是,对于必须遍历链表的少量存储桶(深度可以达到大约 5K 个节点),其性能优于具有更多存储桶且差异大三个数量级的哈希表。
int SMALL_BUCKET_SIZE = 10;
int BIG_BUCKET_SIZE = 10000;
HashTable<String, Integer> smallHashTable = new HashTable<>(SMALL_BUCKET_SIZE);
HashTable<String, Integer> bigHashtTable = new HashTable<>(BIG_BUCKET_SIZE);
我预计较大的哈希表的搜索时间复杂度为 O(1),其中较小的哈希表具有较高的冲突率,由于遍历链接节点而花费更多时间,但下面的数字显示较小的表优于较宽的表表。
获取 SmallTable:0.000007
获取BigTable:0.000018
所以我决定循环我的 HashTable.get 一千次来考虑 JIT 和 JVM 优化。现在我开始看到的数字似乎证实了我的预期。
获取 SmallTable:0.0000013630
获取BigTable:0.0000002560
我的问题是关于我的逻辑的健全性以及这里的其他 Activity 部分。我将我的测试与 HashTable 和底层 Node 结构的实现的链接一起粘贴。
从这里寻找深度/经验的人,他们可能能够提供有关影响此因素的变量的交互式反馈,例如 key 长度和散列冲突率、存储桶密度等。
HashTableTest.java
@Test
public void canInitializeHashTableWithBucketsForPerformance() throws InterruptedException {
double smallTableTime, bigTableTime;
int SMALL_BUCKET_SIZE = 10;
int BIG_BUCKET_SIZE = 10000;
HashTable<String, Integer> smallHashTable = new HashTable<>(SMALL_BUCKET_SIZE);
HashTable<String, Integer> bigHashtTable = new HashTable<>(BIG_BUCKET_SIZE);
List<String> strings = generateRandomStringKeys(1000);
strings.forEach(string -> bigHashtTable.put(string, 10));
strings.forEach(string -> smallHashTable.put(string, 10));
Consumer<String> bigHashGet = bigHashtTable::get;
Consumer<String> smallHashGet = smallHashTable::get;
String theString = strings.get(strings.size() - 1);
smallTableTime = getElapsedTimeFactoringOutJavaOptimization(theString, smallHashGet);
bigTableTime = getElapsedTimeFactoringOutJavaOptimization(theString, bigHashGet);
System.out.println(String.format("Fetch SmallTable: %.10f", smallTableTime));
System.out.println(String.format("Fetch BigTable: %.10f", bigTableTime));
assertTrue(smallTableTime > bigTableTime);
}
public double getElapsedTimeFactoringOutJavaOptimization(String s, Consumer<String> aMethod) {
long start = 0, end = 0;
for (int i = 0; i < 1000; i++) {
start = System.nanoTime();
aMethod.accept(s);
end = System.nanoTime();
}
return (end - start) / 1_000_000_000D;
}
public List<String> generateRandomStringKeys(int numOfRandomKeys) {
List<String> keys = new ArrayList<>();
for (int i = 0; i < numOfRandomKeys; i++) {
byte[] array = new byte[10];
new Random().nextBytes(array);
keys.add(new String(array, Charset.forName("UTF-8")));
}
return keys;
}
可以在此处找到测试 - Github - HashTableTest.java
也可以在这里找到实现 - Github - HashTable.java
最佳答案
这里有很多问题,但其中包括:
nanoTime
差异并不会使您的基准测试有效。说真的,使用JMH。或者至少运行它,比如一千万次。table[getHash(key) % RADIX]
,这基本上意味着无论表有多大,您只使用其中的10个桶并假装其余的不存在.System.identityHashCode
不是一个有用的哈希函数,尤其是在字符串上,尤其是当您希望实际找到其中存在的元素...或不存在时。Node.next
作为字段,因此最好摆脱它。关于java - 关于LinkedList节点的HashTable的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53217763/
我有一个响应移动的应用程序。 监听器似乎在一个 Action 中被调用多次,即如果我将应用程序从监视器的一部分拖到另一部分。 发生这种情况时,我将一些数据存储到哈希表中。每次存储数据时,我都需要存储到
我想对 SAS 哈希表中存储桶的定义进行一些说明。问题正是关于 hashexp 参数。 根据 SAS DOC,hashexp 是: hash对象的内表大小,其中hash表的大小为2n。 HASHEXP
我有许多以整数为键的哈希表,我希望能够在我的 Freemarker 模板中迭代它们,但是,似乎没有任何效果。 我尝试了 Freemarker iterating over hashmap keys 中
C# 中的你好我有两个哈希表对象,其键/值对相同我想检查两个哈希表键/值对是否相等.. 我尝试了 hashtable 的 equal 方法但没有成功 我应该用 foreach 检查所有项目吗? 谢谢
我不太熟悉 HashTable 和使用 HashTable 动态制作 RadioButtons。我可以使用 HashTable 制作 RadioButtons,但无法获取 RadioButtons i
我想知道是否可以这样: Hashtable myhash =new Hashtable(); 其中 String 是一个单词,整数[]是一个包含两个位置的数组,第一个位置是行号,第二个位置是该单词出现
我很好奇为什么会发生错误: scala> import collection.JavaConverters._ import collection.JavaConverters._ scala> va
我在 Hashtable> 中编码了一些对象属性,其中: Integer是主要的关键Hashtable (代表对象编号) 每个 Hashtable分别代表属性name (String)和属性(prop
我说 .Net Hashtable 不同步而 Java Hashtable 同步对吗?并且同时一个Java HashMap 不同步并且有更好的性能? 我正在重写一个在 C# 中大量使用 HashMap
我有一个来自 .Net 的对象,它有一个 SyncHashTable 类型的属性,在没有抛出异常的情况下无法查看。 在线复现: [HashTable]::Synchronized(@{}) 多线更容易
如何获取给定外部哈希表键的内部HashTable的整数值 HashMap map; Hashtable> h = new Has
有谁知道如何在不使用基于 .NET 的 XMLSerializer 的情况下将哈希表转换为 XML 字符串然后再转换回哈希表。当代码在 IE 内部运行并且浏览器的保护模式打开时,XMLSerializ
我在理解这两者之间的区别时遇到了一些困难..这两者都是指向指针的指针吗?另外,它们分别适合在什么情况下使用? 最佳答案 struct node *hash1[MAXSIZE]; struct node
这个问题已经有答案了: Why does java.util.Properties implement Map and not Map (5 个回答) 已关闭 5 年前。 正如标题所述:我想找到为什么
首先,大家好。我已经中途了Python Programming for Finance - Creating targets for machine learning labels ,我有一个 csv
这是我的路线构建器。在这里,我尝试将文件中的数据插入主题。稍后,我将传递我的主要方法并使用 Camel 上下文运行它。我尝试了几个代码,但没有一个对我有帮助。我正在研究 Apache kafka -
当负载因子接近 1 以确保最小的内存浪费时,哪种 hashmap 冲突处理方案更好? 我个人认为答案是使用线性探测进行开放寻址,因为在发生冲突时它不需要任何额外的存储空间。它是否正确? 最佳答案 回答
它们是什么以及它们如何工作? 它们在哪里使用? 我什么时候应该(不)使用它们? 我一遍又一遍地听到这个词,但我不知道它的确切含义。 我听说他们允许关联数组,方法是通过散列函数发送数组键,该函数将其转换
当我们在哈希表中插入/查找键时,教科书说是O(1)时间。但是,怎么可能有O(1)查找时间呢?如果哈希表将 key 存储在向量中,则将花费O(N);如果在二叉树中,则将花费O(logN)。我只是无法使用
这不是针对特定解决方案的特定问题;但这是对以下事实的回应:我找不到有关如何为哈希表和类似任务选择良好的哈希函数的良好堆栈溢出问题。 所以!让我们谈谈散列函数,以及如何选择一种。需要为自己的特定任务选择
我是一名优秀的程序员,十分优秀!