ArrayList and ArrayList Positional Access Efficiency

I just read this sample code: http://robaustin.wikidot.com/how-does-the-performance-of-arraylist-compare-to-array

which causes j = INT_ARRAY [i]; be three times faster than j = ARRAY_LIST.get (i)

I know that ArrayList internally uses an array. Therefore, I would like to know in detail what additional operations are added this time (calling methods, casting, other JVM considerations, etc.)

Thanks in advance.

+3
source share
6 answers

. , , JVM - . - JVM - . , INT_ARRAY.length ArrayList, , JIT . .

, ArrayList.get() :

  • ( , ArrayList ). , , ArrayList.
  • , , JIT
  • - , , .

, . , . ? , , JIT ?

Microbenchmarking - , , .

+10

. .

, , get, , , , Sun JVM , . , ArrayList - .

( Scala, Java ArrayList), , , () :

object ArraySpeed {
  def ptime[A](f: => A) = {
    val t0 = System.nanoTime
    val ans = f
    printf("Elapsed: %.3f seconds\n",(System.nanoTime-t0)*1e-9)
    ans
  }

  val a = Array.range(0,1000000).map(x => new java.lang.Integer(x))
  val b = new java.util.ArrayList[java.lang.Integer]
  a.foreach(x => b.add(x))

  var j = 0

  def jfroma = {
    var i=0
    while (i<1000000) {
      j += a(i).intValue
      i += 1
    }
    j
  }

  def jfromb = {
    var i=0
    while (i<1000000) {
      j += b.get(i).intValue
      i += 1
    }
    j
  }

  def main(args: Array[String]) {
    for (i <- 1 to 5) {
      ptime(for (j <- 1 to 100) yield jfroma)
      ptime(for (j <- 1 to 100) yield jfromb)
      println
    }
  }
}

:

$ scalac ArraySpeed.scala
$ scala ArraySpeed
Elapsed: 0.324 seconds   // This is direct array access
Elapsed: 0.378 seconds   // This is ArrayList

Elapsed: 0.326 seconds
Elapsed: 0.389 seconds

Elapsed: 0.355 seconds
Elapsed: 0.349 seconds

Elapsed: 0.323 seconds
Elapsed: 0.333 seconds

Elapsed: 0.318 seconds
Elapsed: 0.331 seconds

Scala - - Java, . ( scala - java ​​ .)

+3

, ( , JVM, ..), -.

readFromArrayList:

   6:   goto    25
   9:   getstatic       #47; //Field ARRAY_LIST:Ljava/util/List;
   12:  iload_3
   13:  invokeinterface #116,  2; //InterfaceMethod java/util/List.get:(I)Ljava/lang/Object;
   18:  checkcast       #17; //class java/lang/Integer
   21:  astore_0
   22:  iinc    3, 1
   25:  iload_3
   26:  getstatic       #25; //Field INT_ARRAY:[Ljava/lang/Integer;
   29:  arraylength
   30:  if_icmplt       9

readFromArray:

   6:   goto    18
   9:   getstatic       #25; //Field INT_ARRAY:[Ljava/lang/Integer;
   12:  iload_3
   13:  aaload
   14:  astore_0
   15:  iinc    3, 1
   18:  iload_3
   19:  getstatic       #25; //Field INT_ARRAY:[Ljava/lang/Integer;
   22:  arraylength
   23:  if_icmplt       9

, " ", , op # 13: aaload ( ) invokeinterface checkcast ( ArrayList).

+2

, JVM . , - .. javad, , . .

+1

. 3 , differnet: 1. - , . 2. , 1. - - litle - . 3. - " " . : , - . , . 15-25% . ? GC. .

0

, 3 , , JVM, . , , , . , , . arraylist, ,

 public void ensureCapacity(int minCapacity) {
    modCount++;
    int oldCapacity = elementData.length;
    if (minCapacity > oldCapacity) {
        Object oldData[] = elementData;
        int newCapacity = (oldCapacity * 3)/2 + 1;
            if (newCapacity < minCapacity)
        newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
    }
    }

, . , , , .

-2

Source: https://habr.com/ru/post/1793930/


All Articles