001package algs14; 002import java.util.*; 003import stdlib.*; 004public class PlaygroundIndexing { 005 public static void main (String[] args) { 006 ArrayList<Integer> array = new ArrayList<>(); 007 LinkedList<Integer> linked = new LinkedList<>(); 008 int size = 60_000_000; 009 for (int i=1; i<=size; i++) { 010 array.add(i); 011 linked.add(i); 012 } 013 014 int MIN = 256; 015 int MAX = size; 016 double prevTime = performanceLinked (linked, MIN); 017 for (int N = MIN*2; N <= MAX; N=N*2) { 018 double time = performanceLinked (linked, N); 019 StdOut.format ("Linked elapsed count N=%,13d [%10.3f : %10.3f]\n", N, time, time/prevTime); 020 prevTime = time; 021 } 022 023 MIN = 256; 024 MAX = size; 025 prevTime = performanceArray (array, MIN); 026 for (int N = MIN*2; N <= MAX; N=N*2) { 027 double time = performanceArray (array, N); 028 StdOut.format ("Array elapsed count N=%,13d [%10.3f : %10.3f]\n", N, time, time/prevTime); 029 prevTime = time; 030 } 031 } 032 private static double performanceLinked (LinkedList<Integer> linked, int N) { 033 Stopwatch sw = new Stopwatch (); 034 linked.get(N); 035 return sw.elapsedTime (); 036 } 037 private static double performanceArray (ArrayList<Integer> linked, int N) { 038 Stopwatch sw = new Stopwatch (); 039 linked.get(N); 040 return sw.elapsedTime (); 041 } 042 043}