简体   繁体   English

为什么在插入LinkedList和ArrayList时得到关于时间的不同输出

[英]Why getting the different output regarding the time when inserting to LinkedList and ArrayList

I was working with LinkedList and ArrayList and I know the concept of adding the elements into the ArrayList and LinkedList, but I when run code of checking the time of insertion then I got different insertion time again and again, for both LinkedList and ArrayList. 我正在使用LinkedList和ArrayList,我知道将元素添加到ArrayList和LinkedList中的概念,但是当我运行检查插入时间的代码时,我对LinkedList和ArrayList一次又一次地获得不同的插入时间。

Sometimes the insertion time of LinkedList comes better and vice versa, how is it happening exactly, can any one please tell me. 有时LinkedList的插入时间会更好,反之亦然,它是如何发生的,请任何人告诉我。

import java.util.ArrayList;

public class Time
   {

   public static void main(String args[])

    {

       int n=100000;
       long milis = System.currentTimeMillis();
       ArrayList obj=new ArrayList();

    for(int k=0;k<=n;k++)
    {
        obj.add(k);



    }
    System.out.println("insert arraylist takes "
            +(System.currentTimeMillis()-milis)+" ms");
}

  }

Output of this program is 该计划的输出是

1)insert arraylist takes 13 ms 2)insert arraylist takes 9 ms 1)插入arraylist需要13 ms 2)插入arraylist需要9 ms

The second code is 第二个代码是

 import java.util.LinkedList;

  public class Time1 
  {

        public static void main(String args[])
{
      int n=100000;
      long milis = System.currentTimeMillis();
      LinkedList obj=new LinkedList();

    for(int k=0;k<=n;k++)
    {
        obj.add(k);



    }
    System.out.println("insert linklist takes "
           +(System.currentTimeMillis()-milis)+" ms");
}

 }

The output of this 这个的输出

1)insert linklist takes 8 ms 1)插入链接列表需要8毫秒

2)insert linklist takes 17 ms 2)插入链接列表需要17毫秒

Generally speaking, a linked list will be more efficient at adding and removing elements anywhere but the end of the list, but will be much slower at looking up an arbitrary index in the list. 一般来说,链表更有效地添加和删除列表末尾的任何位置,但在查找列表中的任意索引时会慢得多。 To add or remove an element in any location a LinkedList just has to change a couple of references, but in an ArrayList everything after that point needs shifting. 要在任何位置添加或删除元素, LinkedList只需更改几个引用,但在ArrayList ,该点之后的所有内容都需要更改。 In terms of looking up an arbitrary index, an ArrayList simply jumps to that location in memory, a LinkedList must traverse through each item up to that point. 在查找任意索引方面, ArrayList只是跳转到内存中的那个位置, LinkedList必须遍历每个项目直到那一点。

That's the trivial example. 这是一个微不足道的例子。 There's two main reasons why you're not seeing this above: 您没有看到上述内容的原因有两个:

  • Firstly, microbenchmarks suck at the best of times, but especially in a language like Java where you have a JIT that will "warm up" and alter performance in the process. 首先,微基准测试在最好的时候很糟糕,但尤其是在像Java这样的语言中,你有一个JIT会在这个过程中“热身”并改变性能。 Either benchmark it in a real world scenario, or you can pretty much wave goodbye to any real performance metric. 要么在真实场景中对其进行基准测试,要么就可以向任何真实的性能指标挥手告别。

  • Secondly, ArrayList has seen lots of optimisation over the years, to the point that I've even noticed it performing faster in certain cases where you'd classically expect LinkedList to win out. 其次, ArrayList多年来已经看到了很多优化,我甚至注意到它在某些你经常期望LinkedList胜出的情况下表现得更快。

Personally, I'd stick ArrayList in the real world application I'm using, not a micro benchmark, and measure the performance. 就个人而言,我会在我正在使用的真实世界应用程序中使用ArrayList ,而不是微基准测试,并测量性能。 If the performance was unacceptable, I'd switch the implementation to LinkedList (which should be a one line change) and then run a benchmark again to check. 如果性能不可接受,我LinkedList实现切换到LinkedList (应该是一行更改),然后再次运行基准测试。 Without those checks, it's nigh on impossible to say what will perform better in your scenario. 没有这些检查,几乎不可能说出在你的场景中会有更好的表现。

你需要以某种方式使用obj来避免热点优化你的整个for循环。

Everytime you start benchmarking java you have to be very sceptical about your results. 每当您开始对Java进行基准测试时,您都必须对结果持怀疑态度。 Most of times your results will show different results which you could expect. 大多数情况下,您的结果会显示出您可能期望的不同结果。 There are too many factors which can affect performance in your programm. 影响程序性能的因素太多了。 To make a correct benchmark you need deep understanding of JVM. 要制作正确的基准测试,您需要深入了解JVM。 The simpliest points I can name to you: 我可以给你的最简单点:

  • JIT warm up (your first benchmarking results usually will be much worse than later) JIT热身(你的第一个基准测试结果通常会比以后差很多)
  • DCE aka Dead Code Elemination. DCE又名死代码终结。 This is not your case, but it's useful to know about it too 这不是你的情况,但了解它也很有用

To make good benchmark with results which could be correctly interpreted, I recommend to look at Java Harness and carefully check and run all examples . 为了对可以正确解释的结果做出良好的基准测试,我建议您查看Java Harness并仔细检查并运行所有示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM