比较 arraylist 和 linkedlist...我的代码错误吗?
我在另一个问题中问,为什么在读取文件和创建列表时,数组列表似乎比链接列表更快。我现在尝试添加到列表的前面或列表的后面。 Arraylist 仍然更快。
我只是想确保我正确使用这些东西。这就是我正在做的:
public class LinkedListTest {
private List<String> Names;
public LinkedListTest(){
Names = new LinkedList<String>();
}
然后我只使用链接列表方法,即“Names.add(strings)”。当我测试数组列表时,它几乎是相同的:
public class ArrayListTest {
private List<String> Names;
public ArrayListTest(){
Names = new ArrayList<String>();
}
我做得对吗?事实上,在比较速度时,将构造函数方法中的列表类型从 ArrayList 更改为 LinkedList 几乎是我在代码中所做的唯一更改。这是正确的做法吗?
编辑: 哦,我只是在 add 函数之前和之后执行 System.currentTimeMillis() 来测量时间。
I asked in another question why arraylist seemed faster than linkedlist when reading a file and to create the lists. I've now tried adding to the front of the list or the back of the list. Arraylist was still faster.
i just want to make sure i'm using these things right. here's what i'm doing:
public class LinkedListTest {
private List<String> Names;
public LinkedListTest(){
Names = new LinkedList<String>();
}
Then I just using linkedlist methods ie "Names.add(strings)". And when I tested arraylists, it's nearly identical:
public class ArrayListTest {
private List<String> Names;
public ArrayListTest(){
Names = new ArrayList<String>();
}
Am I doing it right? In fact, changing the list type in the constructor method from ArrayList to LinkedList was pretty much the ONLY change I made in the code when comparing the speeds. Is that the right way to go about it?
EDIT:
Oh, and I just do System.currentTimeMillis() before and after the add function to measure time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是如何测试的?通常,它是通过测量数百万次执行相同操作所需的时间,然后进行简单的除法来完成的。
How did you test? Generally it's done by measuring the time it takes to do the same operation millions of time followed by a simple division.
是的,您使用它们是正确的。您使用多少数据进行测试?如果数据集太小,您可能无法获得良好的测试结果,因为可能会出现特殊情况的优化,从而导致结果出现偏差。对于您的计时,您应该对多次测试运行的时间进行平均,以确保 CPU 负载峰值和其他资源峰值得到平均。
我不确定,但我建议尝试不同数量的测试数据 - 可能增加 10 倍,看看性能是否出现线性变化。例如,您可以测试包含 100 个项目、1000、10000、100000 和 1000000 的每个列表,并查看差异是否是线性的以及实现如何比较。
除了在开头和结尾插入之外,测试将项目插入列表中间所需的时间可能会很有趣。
Yeah, you are using those correctly. How much data are you testing with? If the data set is too small, you might not get good test results, because there might be special case optimizations coming into play that skew the results. For your timing, you should average the time over many test runs to be sure CPU load spikes and other resource spikes are averaged out.
I don't know for sure, but I would suggest trying different volumes of test data - maybe increase by factors of 10 and see if you see a linear change in performance. For example, you could test each list with 100 items, 1000, 10000, 100000, and 1000000 and see if the difference is linear and how the implementations compare.
It might be interesting to test the time it takes to insert an item into the middle of the list, in addition to inserting at the beginning and end.
如果您想要简单的东西,简单的时间增量就可以了,但是为了将来的参考,如果您想要更复杂的东西,请查看以下内容:
http://www.ibm.com/developerworks/java/library/j-benchmark1.html
Simple time deltas is fine if you want something simple, however for future reference if you want something more complex check out this:
http://www.ibm.com/developerworks/java/library/j-benchmark1.html