作者:妖精的尾巴的阿鲁哈 | 来源:互联网 | 2023-01-27 17:13
I have a set – a HashSet I want to remove some items from it… none of the items in the "removals" collection will be in the original set.
我有一个集合 - 一个HashSet我想从中删除一些项目......“removals”集合中的所有项目都不在原始集合中。
I specify the size of the "source" set and the size of the "removals" collection on the command line, and build both of them. The source set contains only non-negative integers; the removals set contains only negative integers. I measure how long it takes to remove all the elements using System.currentTimeMillis(), which isn’t the world most accurate stopwatch but is more than adequate in this case, as you’ll see. Here’s the code:
我在命令行中指定“source”集的大小和“removals”集合的大小,并构建它们。源集仅包含非负整数;删除集仅包含负整数。我测量使用System.currentTimeMillis()移除所有元素所需的时间,这不是世界上最精确的秒表,但在这种情况下绰绰有余,正如您将看到的那样。这是代码:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
int sourceSize = Integer.parseInt(args[0]);
int removalsSize = Integer.parseInt(args[1]);
Set source = new HashSet();
Collection removals = new ArrayList();
for (int i = 0; i
Let’s start off by giving it an easy job: a source set of 100 items, and 100 to remove:
让我们开始给它一个简单的工作:100个项目的源集合和100个删除:
c:UsersJonTest>java Test 100 100
Time taken: 1ms
Okay, That's fast as I expected.
好的,这比我想象的要快。
Next i tried source of one million items and 300,000 items to remove?
接下来我尝试了一百万件物品和300,000件物品的来源?
c:UsersJonTest>java Test 1000000 300000
Time taken: 38ms
That still seems pretty speedy. Now make it a bit easier – 300,000 source items and 300,000 removals:
这似乎仍然很快。现在让它更容易 - 300,000个源项目和300,000个删除:
c:UsersJonTest>java Test 300000 300000
Time taken: 178131ms
Nearly three minutes?
差不多三分钟?
Really confused !! can some one explain why this is happening.
真的很困惑!!有人可以解释为什么会这样。
1 个解决方案