- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我基本上是在做以下事情:
ArrayList< ArrayList< String>>
中.对另一个数据库表做同样的事情。
查找所有行 ArrayList< String>
在第二个数据库中的第一个数据库中遍历它并执行 a.contains(b.get(i))
.如果包含是true
然后我做a.remove(b.get(i))
现在,如果我使用 Hashtable< Arraylist< String>> 而不是上面提到的使用 a.containsKey(i.getKey())
的 ArrayList,会快多少? i 是 b 上的迭代器,然后使用 i.remove 删除?是否足以进行更改?
此外,使用 Hashmap 会更谨慎吗?如果是,为什么……
最佳答案
我自下而上的回答:
Hashtable 和 HashMap 之间的区别已在 Differences between HashMap and Hashtable? 中(彻底)讨论过。简短摘要:HashMap 更高效,应该代替 Hashtable 使用。
在散列数据结构中查找数据(contains() 和 remove() 操作)的阶数为 O(log2) - 也就是说,它与数据点数量的 2 对数成正比在结构中。如果有 4 个数据元素,则需要 X 时间;如果有 8 个元素,则需要 2X 时间,16 个元素,则需要 3X 时间,依此类推。哈希结构的数据访问时间增长非常缓慢。
在列表中查找数据的顺序为 O(N) - 即与列表中元素的数量成正比。 1个元素需要Y时间,2个元素需要2Y时间,4个元素需要4Y时间等等。所以时间消耗随着列表的大小线性增长。
因此:如果您必须从数据结构中随机查找大量元素,哈希数据结构是最佳选择,只要:
-数据有一个不错的 hashCode() 实现(ArrayList 的那个是好的)
- 数据有 hashCode() 和 equals() 实现相互匹配,即。如果 a.equals(b) 那么 a.hashCode() == b.hashCode()。 ArrayList 也是如此。
另一方面,如果您正在处理有序 数据,还有其他算法可以显着减少搜索和删除时间。如果数据库中的数据已编制索引,那么在获取数据时使用 ORDER BY 可能是值得的,然后使用算法来排序数据。
总结一下:使用 HashMap 而不是 ArrayList 来获取列表 a。
我写了一个小程序来对问题进行基准测试。结果优先:程序在 Core i5 2.40 GHz CPU 上运行于 Sun JVM 1.6.0_41 for Windows 7,32 位。打印输出:
For 1000 words: List: 1 ms, Map: 2 ms
For 5000 words: List: 15 ms, Map: 12 ms
For 10000 words: List: 57 ms, Map: 12 ms
For 20000 words: List: 217 ms, Map: 37 ms
For 30000 words: List: 485 ms, Map: 45 ms
For 50000 words: List: 1365 ms, Map: 61 ms
性能特征在像这样的简单测试中表现得很好。我运行带有更多数据的 map 版本并得到以下信息:
For 100000 words: List: - ms, Map: 166 ms
For 500000 words: List: - ms, Map: 1130 ms
For 1000000 words: List: - ms, Map: 3540 ms
最后是基准测试代码:
public void benchmarkListVersusMap() {
for (int count : new int[]{1000, 5000, 10000, 20000, 30000, 50000}) {
// Generate random sample data
List<List<String>> words = generateData(count, 10, count);
// Create ArrayList
List<List<String>> list = new ArrayList<List<String>>();
list.addAll(words);
// Create HashMap
Map<List<String>, Boolean> map = new HashMap<List<String>, Boolean>();
for (List<String> row : words) {
map.put(row, true);
}
// Measure:
long timer = System.currentTimeMillis();
for (List<String> row: words) {
if (list.contains(row)) {
list.remove(row);
}
}
long listTime = System.currentTimeMillis() - timer;
timer = System.currentTimeMillis();
for (List<String> row : words) {
if (map.containsKey(row)) {
map.remove(row);
}
}
long mapTime = System.currentTimeMillis() - timer;
System.out.printf("For %s words: List: %s ms, Map: %s ms\n", count, listTime, mapTime);
}
}
private List<List<String>> generateData(int rows, int cols, int noOfDifferentWords) {
List<List<String>> list = new ArrayList<List<String>>(rows);
List<String> dictionary = generateRandomWords(noOfDifferentWords);
Random rnd = new Random();
for (int row = 0; row < rows; row++) {
List<String> l2 = new ArrayList<String>(cols);
for (int col = 0; col < cols; col++) {
l2.add(dictionary.get(rnd.nextInt(noOfDifferentWords)));
}
list.add(l2);
}
return list;
}
private static final String CHARS = "abcdefghijklmnopqrstuvwxyz0123456789";
private List<String> generateRandomWords(int count) {
Random rnd = new Random();
List<String> list = new ArrayList<String>(count);
while (list.size() < count) {
StringBuilder sb = new StringBuilder(20);
for (int i = 0; i < 10; i++) {
sb.append(CHARS.charAt(rnd.nextInt(CHARS.length())));
}
list.add(sb.toString());
}
return list;
}
关于java - Hashtable<ArrayList<String>,boolean> 的 .contains() 方法比 ArrayList<ArrayList<String>> 快多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17543793/
这看起来很基础,但我想不通。是否有一种简单的 CSS 唯一方法可以使 cssa 真正快速淡入并缓慢淡出。这是为了改变多个 div 的颜色。大约 0.5 秒的缓入和 2 秒的缓出。 谢谢! 最佳答案 你
我一直在用各种语言和实现实现相同的代码(在 Blackjack 中发牌而不爆牌的方法的数量)。我注意到的一个奇怪之处是,Python 在 C 中调用分区函数的实现实际上比用 C 编写的整个程序快一点。
如果我没看错,/ 意味着它右边的节点必须是左边节点的直接子节点,例如/ul/li 返回 li 项,它们是作为文档根的 ul 项的直接子项。 //ul//li 返回 li 项,它们是文档中某处任何 ul
如何随机更新一个表。所以你给一列一个随机值。并且该列(例如“顶部”)是唯一的。如果您在数字 10 到 20 之间进行选择,并且您有 10 行,那么您就不能有未使用的数字。如果你有 Test table
这在一小部分是一个问题(因为我不明白为什么它会有所不同),在很大程度上是一篇希望能帮助其他一些可怜的程序员的帖子。 我有一个代码库,是我大约 5-7 年前第一次开始 Android 编程时编写的,它具
我正在尝试过滤关系表以获得满足两个条件的表子集(即:我想要 color_ids 为 1 或 2 的条目的所有 ID)。这是一张结实的 table ,所以我正在尝试尽可能多地进行优化。 我想知道是否有人
在上一篇《聊聊PHP中require_once()函数为什么不好用》中给大家介绍了PHP中require_once()为什么不好用的原因,感兴趣的朋友可以去阅读了解一下~ 那么本文将给大家介绍PH
很难说出这里问的是什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或言辞激烈,无法以目前的形式合理回答。如需帮助澄清此问题以便可以重新打开,visit the help center . 10年前关
有没有办法提高glReadPixels的速度?目前我做: Gdx.gl.glReadPixels(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeig
通常,我以函数形式`:=`()来计算data.table中的多列,认为这是最有效的方法。但是我最近发现它比简单地重复使用:=慢。至少在我的电脑上。 我猜想:=的功能形式可能会产生一些开销,但这是它变慢
我的问题是针对 Windows 环境中多线程的性能问题。 在测试我的代码后,我得到的结果是增加线程数不会提高并行计算的性能,并且在经过一些计数后变得更少。到底是怎么回事?是否可以找出最佳线程数的公式:
我看到很少有相同问题的主题,但我仍然无法解决我的问题。这是我的代码 - 使用 XOR 加密的 C 套接字编程 当服务器和客户端连接时:- 用户发送消息,例如:你好- 服务器响应,例如:(服务器):你好
我正在定义继承自 Shape 类并实现“几何”属性的形状。 这是一个例子: public class Landmark : Shape { public override bool IsInB
相同代码在 Android(1Ghz Snapdragon)上的执行速度比我在 3.3 Ghz Core 2 Duo 的 PC(在桌面应用程序中)快 2 倍(PC 的类被复制到 Android 项目)
我需要将一个值与一组数组进行比较。但是,我需要比较 foreach 中的多个值。如果使用 in_array,它可能会很慢,真的很慢。有没有更快的选择?我当前的代码是 foreach($a as $b)
这个问题在这里已经有了答案: How do I write a correct micro-benchmark in Java? (11 个答案) 关闭 9 年前。 今天我做了一个简单的测试来比较
如果比较不应该以这种方式进行,我深表歉意。我是编程新手,只是很好奇为什么会这样。 我有一个包含词嵌入的大型二进制文件 (4.5gb)。每行都有一个单词,后面跟着它的嵌入,它由 300 个浮点值组成。我
我经历了几个不同的四元数乘法实现,但我很惊讶地发现引用实现是迄今为止我最快的实现。这是有问题的实现: inline static quat multiply(const quat& lhs, cons
我写了一个简单的例子,估计调用虚函数的平均时间,使用基类接口(interface)和dynamic_cast和调用非虚函数。这是它: #include #include #include #in
有没有人知道比“StackWalk”更好/更快的获取调用堆栈的方法?我还认为 stackwalk 在有很多变量的方法上也会变慢......(我想知道商业分析员是做什么的?)我在 Windows 上使用
我是一名优秀的程序员,十分优秀!