LoginSignup
0
0

More than 5 years have passed since last update.

[Java] ArrayDeque

Posted at

ArrayDeque is implemented by array. When capacity increased, it will create a new array and copy the old one to new.

import java.util.*;
import java.lang.reflect.*;

public class ArrayDequePerformance {
  public static void main(String[] args) {
    try {
      long s1 = now();
      ArrayDeque<Integer> q = new ArrayDeque<>();
      for (int i = 0; i < 16777215; ++i) {
        q.add(1);
      }
      long e1 = now();
      System.out.println(e1 - s1);

      Field f = q.getClass().getDeclaredField("elements");
      f.setAccessible (true);
      Object[] ar = (Object[])f.get(q);
      System.out.println(ar.length);

      long s2 = now();
      q.add(1);
      long e2 = now();
      System.out.println(e2 - s2);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public static long now() {
    return System.currentTimeMillis();
  }
}
131
16777216
113
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0