changeset 8258:960b36d46727

Merge
author briangoetz
date Fri, 19 Apr 2013 16:26:16 -0400
parents 36add84c30fd cd84524111dd
children 96be5e1ae7d5
files
diffstat 1 files changed, 68 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/util/Iterator/IteratorDefaults.java	Fri Apr 19 14:15:50 2013 -0400
+++ b/test/java/util/Iterator/IteratorDefaults.java	Fri Apr 19 16:26:16 2013 -0400
@@ -213,6 +213,7 @@
         final List<Integer> source = Arrays.asList(data);
 
         final String[] iterableCollectionClasses = {
+                "java.util.ArrayList", // warmup, results discarded
                 "java.util.ArrayDeque",
                 "java.util.ArrayList",
                 "java.util.HashSet",
@@ -232,6 +233,7 @@
                 "java.util.concurrent.PriorityBlockingQueue"
         };
 
+        boolean warmup = true;
         final int ITERATIONS = 10;
         final Integer[] target = new Integer[source.size()];
         for (final String iterableClass : iterableCollectionClasses) {
@@ -258,6 +260,7 @@
                 }
                 final long iteratorEnd = System.nanoTime();
 
+                if (warmup) { continue; } // warmup, discard results
                 final long forEachTime = forEachEnd - forEachStart;
                 final long iteratorTime = iteratorEnd - iteratorStart;
                 final long speedup = iteratorTime - forEachTime;
@@ -272,6 +275,71 @@
                 System.out.print((speedup * 100) / iteratorTime);
                 System.out.print("%)\n");
             }
+            if (warmup) { warmup = false; }
+            System.out.println();
+        }
+    }
+
+    @Test(enabled = false)
+    public void compareSubListForEachPerformance() throws Exception {
+        final Integer[] data = new Integer[1000 * 100];
+        for (int i = 0; i < data.length; i++) {
+            data[i] = i;
+        }
+        final List<Integer> source = Arrays.asList(data);
+
+        final String[] listClasses = {
+                "java.util.ArrayList", // warmup, results discarded
+                "java.util.ArrayList",
+                "java.util.LinkedList",
+                "java.util.Vector",
+                "java.util.concurrent.CopyOnWriteArrayList"
+        };
+
+        boolean warmup = true;
+        final int ITERATIONS = 10;
+        final Integer[] target = new Integer[source.size()];
+        for (final String listClass : listClasses) {
+            final Class<? extends List<Integer >> type =
+                    (Class<? extends List<Integer>>) Class.forName(listClass);
+            final Constructor<? extends List<Integer >> copyConstructor =
+                    type.getConstructor(Collection.class);
+            final List<Integer> iterable = copyConstructor.newInstance(source);
+            final List<Integer> reference = Collections.unmodifiableList(iterable);
+
+            for (int i = 0; i < ITERATIONS; i++) {
+                final Iterator<Integer> iterator = reference.subList(42, reference.size() - 37).iterator();
+                final long forEachStart = System.nanoTime();
+                iterator.forEachRemaining(x -> {
+                    target[x.intValue()] = x;
+                });
+                final long forEachEnd = System.nanoTime();
+
+                final Iterator<Integer> iterator2 = reference.iterator();
+                Integer x;
+                final long iteratorStart = System.nanoTime();
+                while (iterator2.hasNext()) {
+                    x = iterator2.next();
+                    target[x.intValue()] = x;
+                }
+                final long iteratorEnd = System.nanoTime();
+
+                if (warmup) { continue; } // warmup, discard results
+                final long forEachTime = forEachEnd - forEachStart;
+                final long iteratorTime = iteratorEnd - iteratorStart;
+                final long speedup = iteratorTime - forEachTime;
+                System.out.print(listClass);
+                System.out.print(" iterator: ");
+                System.out.print(iteratorTime);
+                System.out.print(", forEach: ");
+                System.out.print(forEachTime);
+                System.out.print(", speedup: ");
+                System.out.print(speedup);
+                System.out.print(" (");
+                System.out.print((speedup * 100) / iteratorTime);
+                System.out.print("%)\n");
+            }
+            if (warmup) { warmup = false; }
             System.out.println();
         }
     }