Stepan Koltsov avatar Stepan Koltsov committed 12de2dd

rename stuff

Comments (0)

Files changed (16)

src/java/org/jetbrains/jet/iteratorListPerf/Filter.java

-package org.jetbrains.jet.iteratorListPerf;
-
-import org.jetbrains.jet.iteratorListPerf.lib.CollectionUtils;
-
-import java.util.List;
-
-/**
- * @author Stepan Koltsov
- */
-public class Filter {
-    // -client: https://gist.github.com/3fe1c2b48b3c216de5c2
-    // -server: https://gist.github.com/ea6f4b66a3d3ee9c5d77
-    public static void main(String[] args) {
-        TestUtils.printenv(Filter.class);
-        for (;;) {
-            runTest();
-        }
-    }
-
-    private static void runTest() {
-        runTestPairs(10, 15);
-        runTestPairs(15, 150);
-        runTestPairs( 0, 2000000);
-        runTestPairs( 1, 2000000);
-        runTestPairs( 2, 2000000);
-        runTestPairs( 3, 2000000);
-        runTestPairs( 7, 1000000);
-        runTestPairs(10, 1500000);
-        runTestPairs(13, 1500000);
-        runTestPairs(100, 150000);
-        runTestPairs(323, 150000);
-        runTestPairs(10000, 1500);
-        runTestPairs(15000, 1000);
-        System.out.println("$");
-    }
-
-    private static void runTestPairs(int collectionSize, int iterationCount) {
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-    }
-
-    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.collect(CollectionUtils.filterToIterator(list.iterator(), TestUtils.positiveF()));
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
-        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-    private static void runTestWithToList(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.filterToList(list, TestUtils.positiveF());
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
-        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-}

src/java/org/jetbrains/jet/iteratorListPerf/Map.java

-package org.jetbrains.jet.iteratorListPerf;
-
-import org.jetbrains.jet.iteratorListPerf.lib.CollectionUtils;
-
-import java.util.List;
-
-/**
- * @author Stepan Koltsov
- */
-public class Map {
-    // -client: https://gist.github.com/0f10a736e4a9b65f4c76
-    // -server: https://gist.github.com/9424905d6175d9e847ed
-    public static void main(String[] args) {
-        TestUtils.printenv(Map.class);
-        for (;;) {
-            runTest();
-        }
-    }
-
-    private static void runTest() {
-        runTestPairs(10, 15);
-        runTestPairs(15, 150);
-        runTestPairs( 0, 2000000);
-        runTestPairs( 1, 2000000);
-        runTestPairs( 2, 2000000);
-        runTestPairs( 3, 2000000);
-        runTestPairs( 7, 1000000);
-        runTestPairs(10, 1500000);
-        runTestPairs(13, 1500000);
-        runTestPairs(100, 150000);
-        runTestPairs(323, 150000);
-        runTestPairs(10000, 1500);
-        runTestPairs(15000, 1000);
-        System.out.println("$");
-    }
-
-    private static void runTestPairs(int collectionSize, int iterationCount) {
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-    }
-
-    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.collect(CollectionUtils.mapToIterator(list.iterator(), TestUtils.plus1F()));
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
-        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-    private static void runTestWithToList(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.mapToList(list, TestUtils.plus1F());
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
-        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-}

src/java/org/jetbrains/jet/iteratorListPerf/MapFilter.java

-package org.jetbrains.jet.iteratorListPerf;
-
-import org.jetbrains.jet.iteratorListPerf.lib.CollectionUtils;
-
-import java.util.List;
-
-/**
- * @author Stepan Koltsov
- */
-public class MapFilter {
-    // -client: https://gist.github.com/e063bb58128cf507a778
-    // -server: https://gist.github.com/ac64e2faae3f1b25f066
-    public static void main(String[] args) {
-        TestUtils.printenv(MapFilter.class);
-        for (;;) {
-            runTest();
-        }
-    }
-    
-    private static void runTest() {
-        runTestPairs(10, 15);
-        runTestPairs(15, 150);
-        runTestPairs( 0, 2000000);
-        runTestPairs( 1, 2000000);
-        runTestPairs( 2, 2000000);
-        runTestPairs( 3, 2000000);
-        runTestPairs( 7, 1000000);
-        runTestPairs(10, 1500000);
-        runTestPairs(13, 1500000);
-        runTestPairs(100, 150000);
-        runTestPairs(323, 150000);
-        runTestPairs(10000, 1500);
-        runTestPairs(15000, 1000);
-        System.out.println("$");
-    }
-
-    private static void runTestPairs(int collectionSize, int iterationCount) {
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-    }
-    
-    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.collect(CollectionUtils.filterToIterator(CollectionUtils.mapToIterator(list.iterator(), TestUtils.plus1F()), TestUtils.positiveF()));
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 2;
-        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-    
-    private static void runTestWithToList(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.filterToList(CollectionUtils.mapToList(list, TestUtils.plus1F()), TestUtils.positiveF());
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 2;
-        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-}

src/java/org/jetbrains/jet/iteratorListPerf/MapFilterMapFilter.java

-package org.jetbrains.jet.iteratorListPerf;
-
-import org.jetbrains.jet.iteratorListPerf.lib.CollectionUtils;
-
-import java.util.List;
-
-/**
- * @author Stepan Koltsov
- */
-public class MapFilterMapFilter {
-    // client: https://gist.github.com/5851e1cb731700f7cdc8
-    // server: https://gist.github.com/9880d3ea62e643a6314f
-    public static void main(String[] args) {
-        TestUtils.printenv(MapFilter.class);
-        for (;;) {
-            runTest();
-        }
-    }
-
-    private static void runTest() {
-        runTestPairs(10, 15);
-        runTestPairs(15, 150);
-        runTestPairs( 0, 2000000);
-        runTestPairs( 1, 2000000);
-        runTestPairs( 2, 2000000);
-        runTestPairs( 3, 2000000);
-        runTestPairs( 7, 1000000);
-        runTestPairs(10, 1500000);
-        runTestPairs(13, 1500000);
-        runTestPairs(100, 150000);
-        runTestPairs(323, 150000);
-        runTestPairs(10000, 1500);
-        runTestPairs(15000, 1000);
-        System.out.println("$");
-    }
-
-    private static void runTestPairs(int collectionSize, int iterationCount) {
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-        runTestWithToIterator(collectionSize, iterationCount);
-        runTestWithToList(collectionSize, iterationCount);
-    }
-
-    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r = CollectionUtils.collect(
-                    CollectionUtils.filterToIterator(
-                            CollectionUtils.mapToIterator(
-                                    CollectionUtils.filterToIterator(
-                                                CollectionUtils.mapToIterator(list.iterator(), TestUtils.plus1F()),
-                                                TestUtils.positiveF()),
-                                    TestUtils.plus1F()),
-                            TestUtils.positiveF())
-            );
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 4;
-        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-    private static void runTestWithToList(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.currentTimeMillis();
-            List<Integer> r =
-                    CollectionUtils.filterToList(
-                            CollectionUtils.mapToList(
-                                    CollectionUtils.filterToList(
-                                            CollectionUtils.mapToList(list, TestUtils.plus1F()),
-                                    TestUtils.positiveF()),
-                            TestUtils.plus1F()),
-                    TestUtils.positiveF());
-            totalDuration += System.currentTimeMillis() - start;
-            junk += TestUtils.sum(r);
-        }
-        long nsPerOp = totalDuration * 1000000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 4;
-        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
-    }
-
-}

src/java/org/jetbrains/jet/iteratorListPerf/MapOptimizedVsMapSimple.java

-package org.jetbrains.jet.iteratorListPerf;
-
-import org.jetbrains.jet.iteratorListPerf.lib.CollectionUtils;
-
-import java.io.FileOutputStream;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Random;
-
-/**
- * @author Stepan Koltsov
- */
-public class MapOptimizedVsMapSimple {
-
-    // https://gist.github.com/cb51a8c63d68924171b3
-
-    public static void main(String[] args) throws Exception {
-        TestUtils.printenv(MapOptimizedVsMapSimple.class);
-        for (int i = 0; ; ++i) {
-            runTest(i);
-        }
-    }
-
-    private static void runTest(int iteration) throws Exception {
-
-        int[][] params = {
-                { 10,      15 },
-                { 15,     150 },
-                {  0, 4000000 },
-                {  1, 4000000 },
-                {  2, 3000000 },
-                {  3, 3000000 },
-                {  5, 5000000 },
-                {  7, 1000000 },
-                { 10, 2500000 },
-                { 13, 2000000 },
-                { 50,  900000 },
-                { 100, 150000 },
-                { 323, 150000 },
-                { 2000, 20000 },
-                { 10000, 2500 },
-                { 15000, 2000 },
-                { 170000, 100 },
-        };
-
-        StringBuilder report = new StringBuilder();
-        report.append("iteration " + iteration + "\n");
-        
-        for (int[] param : params) {
-            runTestPairs(param[0], param[1], report);
-        }
-
-        FileOutputStream os = new FileOutputStream(MapOptimizedVsMapSimple.class.getSimpleName() + ".txt");
-        try {
-            os.write(report.toString().getBytes("utf-8"));
-            os.close();
-        } finally {
-            os.close();
-        }
-
-        System.out.println("$ " + (iteration + 1));
-    }
-    
-    private static final Random random = new Random();
-    
-    private static java.util.Map<String, Long> mins = new HashMap<String, Long>();
-    
-    private enum Mode {
-        OPTIMIZED,
-        SIMPLE,
-    }
-    
-    private static String key(int cs, int ic, Mode mode) {
-        return "cs=" + cs + "; ic=" + ic + "; mode=" + mode;
-    }
-    
-    private static void putMin(String key, long min) {
-        Long oldMin = mins.get(key);
-        if (oldMin == null || oldMin.longValue() > min) {
-            mins.put(key, min);
-        }
-    }
-
-    private static void runTestPairs(int collectionSize, int iterationCount, StringBuilder report) {
-        String q = "cs=" + collectionSize + "; ic=" + iterationCount + "\n";
-        report.append(q);
-        System.out.print(q);
-
-        int optimizedCount = 0;
-        int simpleCount = 0;
-
-        String optimizedKey = key(collectionSize, iterationCount, Mode.OPTIMIZED);
-        String simpleKey = key(collectionSize, iterationCount, Mode.SIMPLE);
-        
-        while (optimizedCount < 3 || simpleCount < 3) {
-            if (random.nextBoolean()) {
-                long d = runTestWithOptimized(collectionSize, iterationCount);
-                putMin(optimizedKey, d);
-                ++optimizedCount;
-            } else {
-                long d = runTestWithSimple(collectionSize, iterationCount);
-                putMin(simpleKey, d);
-                ++simpleCount;
-            }
-        }
-        String r =
-                "min simple   : " + mins.get(simpleKey) + "ps\n" +
-                "min optimized: " + mins.get(optimizedKey) + "ps\n" +
-                "";
-        System.out.print(r);
-        report.append(r);
-        
-    }
-
-    private static long runTestWithSimple(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.nanoTime();
-            List<Integer> r = CollectionUtils.mapToListSimple(list, TestUtils.plus1F());
-            totalDuration += System.nanoTime() - start;
-            junk += TestUtils.sum(r);
-        }
-        long psPerOp = totalDuration * 1000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
-        System.out.println("simple   : " + psPerOp + "ps; " + junk);
-        return psPerOp;
-    }
-
-    private static long runTestWithOptimized(int collectionSize, int iterationCount) {
-        long totalDuration = 0;
-        int junk = 0;
-        for (int i = 0; i < iterationCount; ++i) {
-            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
-            long start = System.nanoTime();
-            List<Integer> r = CollectionUtils.mapToList(list, TestUtils.plus1F());
-            totalDuration += System.nanoTime() - start;
-            junk += TestUtils.sum(r);
-        }
-        long psPerOp = totalDuration * 1000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
-        System.out.println("optimized: " + psPerOp + "ps; " + junk);
-        return psPerOp;
-    }
-
-}

src/java/org/jetbrains/jet/iteratorListPerf/TestUtils.java

-package org.jetbrains.jet.iteratorListPerf;
-
-import org.jetbrains.jet.iteratorListPerf.lib.Function;
-
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * @author Stepan Koltsov
- */
-public class TestUtils {
-    public static void printenv(Class<?> testClass) {
-        System.out.println(testClass.getName());
-        System.out.println(System.getProperty("java.version"));
-    }
-    
-    public static List<Integer> makeListWithInts(int count, int seed) {
-        List<Integer> r = new ArrayList<Integer>();
-        for (int i = 0; i < count; ++i) {
-            r.add(hash(i, seed));
-        }
-        return r;
-    }
-
-    public static int sum(List<Integer> list) {
-        int r = 0;
-        for (Integer i : list) {
-            r += i;
-        }
-        return r;
-    }
-
-    public static int hash(int i, int seed) {
-        return i * seed * 0x9e3779b1;
-    }
-
-    public static Function<Integer, Integer> plus1F() {
-        return new Function<Integer, Integer>() {
-            @Override
-            public Integer apply(Integer integer) {
-                return integer + 1;
-            }
-        };
-    }
-
-    public static Function<Integer, Boolean> positiveF() {
-        return new Function<Integer, Boolean>() {
-            @Override
-            public Boolean apply(Integer integer) {
-                return integer > 0;
-            }
-        };
-    }
-}

src/java/org/jetbrains/jet/iteratorListPerf/lib/CollectionUtils.java

-package org.jetbrains.jet.iteratorListPerf.lib;
-
-import sun.org.mozilla.javascript.internal.FunctionObject;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-import java.util.RandomAccess;
-
-/**
- * @author Stepan Koltsov
- */
-public class CollectionUtils {
-    public static <A, R> Iterator<R> mapToIterator(final Iterator<A> source, final Function<A, R> f) {
-        return new Iterator<R>() {
-            @Override
-            public boolean hasNext() {
-                return source.hasNext();
-            }
-
-            @Override
-            public R next() {
-                return f.apply(source.next());
-            }
-
-            @Override
-            public void remove() {
-                throw new IllegalStateException();
-            }
-        };
-    }
-    
-    public static <A> Iterator<A> filterToIterator(final Iterator<A> source, final Function<A, Boolean> f) {
-        return new Iterator<A>() {
-            private A next;
-            private boolean hasNext;
-            private boolean fetched = false;
-            
-            private void prefetch() {
-                if (this.fetched) {
-                    return;
-                }
-                for (;;) {
-                    if (!source.hasNext()) {
-
-                        this.hasNext = false;
-                        this.fetched = true;
-                        return;
-                    }
-                    A next = source.next();
-                    if (f.apply(next)) {
-                        this.next = next;
-                        this.hasNext = true;
-                        this.fetched = true;
-                        return;
-                    }
-                }
-            }
-            
-            @Override
-            public boolean hasNext() {
-                prefetch();
-                return hasNext;
-            }
-
-            @Override
-            public A next() {
-                if (!hasNext()) {
-                    throw new IllegalStateException();
-                }
-                fetched = false;
-                return next;
-            }
-
-            @Override
-            public void remove() {
-                throw new IllegalStateException();
-            }
-        };
-    }
-    
-    public static <A> List<A> collect(Iterator<A> iterator) {
-        List<A> r = new ArrayList<A>();
-        while (iterator.hasNext()) {
-            r.add(iterator.next());
-        }
-        return r;
-    }
-    
-    public static <A, R> List<R> mapToList(Iterable<A> source, Function<A, R> f) {
-        List<R> result;
-        if (source instanceof ArrayList<?>) {
-            // NOTE: optimization of most frequent operation
-            result = new ArrayList<R>(((ArrayList<?>) source).size());
-        } else {
-            result = new ArrayList<R>();
-        }
-        for (A item : source) {
-            result.add(f.apply(item));
-        }
-        return result;
-    }
-
-    public static <A, R> List<R> mapToListSimple(Iterable<A> source, Function<A, R> f) {
-        ArrayList<R> result = new ArrayList<R>();
-        for (A item : source) {
-            result.add(f.apply(item));
-        }
-        result.trimToSize();
-        return result;
-    }
-
-    public static <A> List<A> filterToList(Iterable<A> source, Function<A, Boolean> f) {
-        ArrayList<A> result;
-        if (source instanceof ArrayList<?>) {
-            result = new ArrayList<A>(((ArrayList<?>) source).size());
-        } else {
-            result = new ArrayList<A>();
-        }
-        for (A item : source) {
-            if (f.apply(item)) {
-                result.add(item);
-            }
-        }
-        result.trimToSize();
-        return result;
-    }
-}

src/java/org/jetbrains/jet/iteratorListPerf/lib/Function.java

-package org.jetbrains.jet.iteratorListPerf.lib;
-
-/**
- * @author Stepan Koltsov
- */
-public interface Function<A, R> {
-    R apply(A a);
-}

src/java/org/jetbrains/jet/perf/iteratorList/Filter.java

+package org.jetbrains.jet.perf.iteratorList;
+
+import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
+
+import java.util.List;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class Filter {
+    // -client: https://gist.github.com/3fe1c2b48b3c216de5c2
+    // -server: https://gist.github.com/ea6f4b66a3d3ee9c5d77
+    public static void main(String[] args) {
+        TestUtils.printenv(Filter.class);
+        for (;;) {
+            runTest();
+        }
+    }
+
+    private static void runTest() {
+        runTestPairs(10, 15);
+        runTestPairs(15, 150);
+        runTestPairs( 0, 2000000);
+        runTestPairs( 1, 2000000);
+        runTestPairs( 2, 2000000);
+        runTestPairs( 3, 2000000);
+        runTestPairs( 7, 1000000);
+        runTestPairs(10, 1500000);
+        runTestPairs(13, 1500000);
+        runTestPairs(100, 150000);
+        runTestPairs(323, 150000);
+        runTestPairs(10000, 1500);
+        runTestPairs(15000, 1000);
+        System.out.println("$");
+    }
+
+    private static void runTestPairs(int collectionSize, int iterationCount) {
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+    }
+
+    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.collect(CollectionUtils.filterToIterator(list.iterator(), TestUtils.positiveF()));
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
+        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+    private static void runTestWithToList(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.filterToList(list, TestUtils.positiveF());
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
+        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+}

src/java/org/jetbrains/jet/perf/iteratorList/Map.java

+package org.jetbrains.jet.perf.iteratorList;
+
+import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
+
+import java.util.List;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class Map {
+    // -client: https://gist.github.com/0f10a736e4a9b65f4c76
+    // -server: https://gist.github.com/9424905d6175d9e847ed
+    public static void main(String[] args) {
+        TestUtils.printenv(Map.class);
+        for (;;) {
+            runTest();
+        }
+    }
+
+    private static void runTest() {
+        runTestPairs(10, 15);
+        runTestPairs(15, 150);
+        runTestPairs( 0, 2000000);
+        runTestPairs( 1, 2000000);
+        runTestPairs( 2, 2000000);
+        runTestPairs( 3, 2000000);
+        runTestPairs( 7, 1000000);
+        runTestPairs(10, 1500000);
+        runTestPairs(13, 1500000);
+        runTestPairs(100, 150000);
+        runTestPairs(323, 150000);
+        runTestPairs(10000, 1500);
+        runTestPairs(15000, 1000);
+        System.out.println("$");
+    }
+
+    private static void runTestPairs(int collectionSize, int iterationCount) {
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+    }
+
+    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.collect(CollectionUtils.mapToIterator(list.iterator(), TestUtils.plus1F()));
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
+        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+    private static void runTestWithToList(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.mapToList(list, TestUtils.plus1F());
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
+        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+}

src/java/org/jetbrains/jet/perf/iteratorList/MapFilter.java

+package org.jetbrains.jet.perf.iteratorList;
+
+import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
+
+import java.util.List;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class MapFilter {
+    // -client: https://gist.github.com/e063bb58128cf507a778
+    // -server: https://gist.github.com/ac64e2faae3f1b25f066
+    public static void main(String[] args) {
+        TestUtils.printenv(MapFilter.class);
+        for (;;) {
+            runTest();
+        }
+    }
+    
+    private static void runTest() {
+        runTestPairs(10, 15);
+        runTestPairs(15, 150);
+        runTestPairs( 0, 2000000);
+        runTestPairs( 1, 2000000);
+        runTestPairs( 2, 2000000);
+        runTestPairs( 3, 2000000);
+        runTestPairs( 7, 1000000);
+        runTestPairs(10, 1500000);
+        runTestPairs(13, 1500000);
+        runTestPairs(100, 150000);
+        runTestPairs(323, 150000);
+        runTestPairs(10000, 1500);
+        runTestPairs(15000, 1000);
+        System.out.println("$");
+    }
+
+    private static void runTestPairs(int collectionSize, int iterationCount) {
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+    }
+    
+    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.collect(CollectionUtils.filterToIterator(CollectionUtils.mapToIterator(list.iterator(), TestUtils.plus1F()), TestUtils.positiveF()));
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 2;
+        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+    
+    private static void runTestWithToList(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.filterToList(CollectionUtils.mapToList(list, TestUtils.plus1F()), TestUtils.positiveF());
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 2;
+        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+}

src/java/org/jetbrains/jet/perf/iteratorList/MapFilterMapFilter.java

+package org.jetbrains.jet.perf.iteratorList;
+
+import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
+
+import java.util.List;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class MapFilterMapFilter {
+    // client: https://gist.github.com/5851e1cb731700f7cdc8
+    // server: https://gist.github.com/9880d3ea62e643a6314f
+    public static void main(String[] args) {
+        TestUtils.printenv(MapFilter.class);
+        for (;;) {
+            runTest();
+        }
+    }
+
+    private static void runTest() {
+        runTestPairs(10, 15);
+        runTestPairs(15, 150);
+        runTestPairs( 0, 2000000);
+        runTestPairs( 1, 2000000);
+        runTestPairs( 2, 2000000);
+        runTestPairs( 3, 2000000);
+        runTestPairs( 7, 1000000);
+        runTestPairs(10, 1500000);
+        runTestPairs(13, 1500000);
+        runTestPairs(100, 150000);
+        runTestPairs(323, 150000);
+        runTestPairs(10000, 1500);
+        runTestPairs(15000, 1000);
+        System.out.println("$");
+    }
+
+    private static void runTestPairs(int collectionSize, int iterationCount) {
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+        runTestWithToIterator(collectionSize, iterationCount);
+        runTestWithToList(collectionSize, iterationCount);
+    }
+
+    private static void runTestWithToIterator(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r = CollectionUtils.collect(
+                    CollectionUtils.filterToIterator(
+                            CollectionUtils.mapToIterator(
+                                    CollectionUtils.filterToIterator(
+                                                CollectionUtils.mapToIterator(list.iterator(), TestUtils.plus1F()),
+                                                TestUtils.positiveF()),
+                                    TestUtils.plus1F()),
+                            TestUtils.positiveF())
+            );
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 4;
+        System.out.println("iterator: " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+    private static void runTestWithToList(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.currentTimeMillis();
+            List<Integer> r =
+                    CollectionUtils.filterToList(
+                            CollectionUtils.mapToList(
+                                    CollectionUtils.filterToList(
+                                            CollectionUtils.mapToList(list, TestUtils.plus1F()),
+                                    TestUtils.positiveF()),
+                            TestUtils.plus1F()),
+                    TestUtils.positiveF());
+            totalDuration += System.currentTimeMillis() - start;
+            junk += TestUtils.sum(r);
+        }
+        long nsPerOp = totalDuration * 1000000000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount / 4;
+        System.out.println("list    : " + nsPerOp + "; cs=" + collectionSize + "; ic=" + iterationCount + "; " + junk);
+    }
+
+}

src/java/org/jetbrains/jet/perf/iteratorList/MapOptimizedVsMapSimple.java

+package org.jetbrains.jet.perf.iteratorList;
+
+import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
+
+import java.io.FileOutputStream;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Random;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class MapOptimizedVsMapSimple {
+
+    // https://gist.github.com/cb51a8c63d68924171b3
+
+    public static void main(String[] args) throws Exception {
+        TestUtils.printenv(MapOptimizedVsMapSimple.class);
+        for (int i = 0; ; ++i) {
+            runTest(i);
+        }
+    }
+
+    private static void runTest(int iteration) throws Exception {
+
+        int[][] params = {
+                { 10,      15 },
+                { 15,     150 },
+                {  0, 4000000 },
+                {  1, 4000000 },
+                {  2, 3000000 },
+                {  3, 3000000 },
+                {  5, 5000000 },
+                {  7, 1000000 },
+                { 10, 2500000 },
+                { 13, 2000000 },
+                { 50,  900000 },
+                { 100, 150000 },
+                { 323, 150000 },
+                { 2000, 20000 },
+                { 10000, 2500 },
+                { 15000, 2000 },
+                { 170000, 100 },
+        };
+
+        StringBuilder report = new StringBuilder();
+        report.append("iteration " + iteration + "\n");
+        
+        for (int[] param : params) {
+            runTestPairs(param[0], param[1], report);
+        }
+
+        FileOutputStream os = new FileOutputStream(MapOptimizedVsMapSimple.class.getSimpleName() + ".txt");
+        try {
+            os.write(report.toString().getBytes("utf-8"));
+            os.close();
+        } finally {
+            os.close();
+        }
+
+        System.out.println("$ " + (iteration + 1));
+    }
+    
+    private static final Random random = new Random();
+    
+    private static java.util.Map<String, Long> mins = new HashMap<String, Long>();
+    
+    private enum Mode {
+        OPTIMIZED,
+        SIMPLE,
+    }
+    
+    private static String key(int cs, int ic, Mode mode) {
+        return "cs=" + cs + "; ic=" + ic + "; mode=" + mode;
+    }
+    
+    private static void putMin(String key, long min) {
+        Long oldMin = mins.get(key);
+        if (oldMin == null || oldMin.longValue() > min) {
+            mins.put(key, min);
+        }
+    }
+
+    private static void runTestPairs(int collectionSize, int iterationCount, StringBuilder report) {
+        String q = "cs=" + collectionSize + "; ic=" + iterationCount + "\n";
+        report.append(q);
+        System.out.print(q);
+
+        int optimizedCount = 0;
+        int simpleCount = 0;
+
+        String optimizedKey = key(collectionSize, iterationCount, Mode.OPTIMIZED);
+        String simpleKey = key(collectionSize, iterationCount, Mode.SIMPLE);
+        
+        while (optimizedCount < 3 || simpleCount < 3) {
+            if (random.nextBoolean()) {
+                long d = runTestWithOptimized(collectionSize, iterationCount);
+                putMin(optimizedKey, d);
+                ++optimizedCount;
+            } else {
+                long d = runTestWithSimple(collectionSize, iterationCount);
+                putMin(simpleKey, d);
+                ++simpleCount;
+            }
+        }
+        String r =
+                "min simple   : " + mins.get(simpleKey) + "ps\n" +
+                "min optimized: " + mins.get(optimizedKey) + "ps\n" +
+                "";
+        System.out.print(r);
+        report.append(r);
+        
+    }
+
+    private static long runTestWithSimple(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.nanoTime();
+            List<Integer> r = CollectionUtils.mapToListSimple(list, TestUtils.plus1F());
+            totalDuration += System.nanoTime() - start;
+            junk += TestUtils.sum(r);
+        }
+        long psPerOp = totalDuration * 1000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
+        System.out.println("simple   : " + psPerOp + "ps; " + junk);
+        return psPerOp;
+    }
+
+    private static long runTestWithOptimized(int collectionSize, int iterationCount) {
+        long totalDuration = 0;
+        int junk = 0;
+        for (int i = 0; i < iterationCount; ++i) {
+            List<Integer> list = TestUtils.makeListWithInts(collectionSize, i);
+            long start = System.nanoTime();
+            List<Integer> r = CollectionUtils.mapToList(list, TestUtils.plus1F());
+            totalDuration += System.nanoTime() - start;
+            junk += TestUtils.sum(r);
+        }
+        long psPerOp = totalDuration * 1000 / (collectionSize > 0 ? collectionSize : 1) / iterationCount;
+        System.out.println("optimized: " + psPerOp + "ps; " + junk);
+        return psPerOp;
+    }
+
+}

src/java/org/jetbrains/jet/perf/iteratorList/TestUtils.java

+package org.jetbrains.jet.perf.iteratorList;
+
+import org.jetbrains.jet.perf.iteratorList.lib.Function;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class TestUtils {
+    public static void printenv(Class<?> testClass) {
+        System.out.println(testClass.getName());
+        System.out.println(System.getProperty("java.version"));
+    }
+    
+    public static List<Integer> makeListWithInts(int count, int seed) {
+        List<Integer> r = new ArrayList<Integer>();
+        for (int i = 0; i < count; ++i) {
+            r.add(hash(i, seed));
+        }
+        return r;
+    }
+
+    public static int sum(List<Integer> list) {
+        int r = 0;
+        for (Integer i : list) {
+            r += i;
+        }
+        return r;
+    }
+
+    public static int hash(int i, int seed) {
+        return i * seed * 0x9e3779b1;
+    }
+
+    public static Function<Integer, Integer> plus1F() {
+        return new Function<Integer, Integer>() {
+            @Override
+            public Integer apply(Integer integer) {
+                return integer + 1;
+            }
+        };
+    }
+
+    public static Function<Integer, Boolean> positiveF() {
+        return new Function<Integer, Boolean>() {
+            @Override
+            public Boolean apply(Integer integer) {
+                return integer > 0;
+            }
+        };
+    }
+}

src/java/org/jetbrains/jet/perf/iteratorList/lib/CollectionUtils.java

+package org.jetbrains.jet.perf.iteratorList.lib;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class CollectionUtils {
+    public static <A, R> Iterator<R> mapToIterator(final Iterator<A> source, final Function<A, R> f) {
+        return new Iterator<R>() {
+            @Override
+            public boolean hasNext() {
+                return source.hasNext();
+            }
+
+            @Override
+            public R next() {
+                return f.apply(source.next());
+            }
+
+            @Override
+            public void remove() {
+                throw new IllegalStateException();
+            }
+        };
+    }
+    
+    public static <A> Iterator<A> filterToIterator(final Iterator<A> source, final Function<A, Boolean> f) {
+        return new Iterator<A>() {
+            private A next;
+            private boolean hasNext;
+            private boolean fetched = false;
+            
+            private void prefetch() {
+                if (this.fetched) {
+                    return;
+                }
+                for (;;) {
+                    if (!source.hasNext()) {
+
+                        this.hasNext = false;
+                        this.fetched = true;
+                        return;
+                    }
+                    A next = source.next();
+                    if (f.apply(next)) {
+                        this.next = next;
+                        this.hasNext = true;
+                        this.fetched = true;
+                        return;
+                    }
+                }
+            }
+            
+            @Override
+            public boolean hasNext() {
+                prefetch();
+                return hasNext;
+            }
+
+            @Override
+            public A next() {
+                if (!hasNext()) {
+                    throw new IllegalStateException();
+                }
+                fetched = false;
+                return next;
+            }
+
+            @Override
+            public void remove() {
+                throw new IllegalStateException();
+            }
+        };
+    }
+    
+    public static <A> List<A> collect(Iterator<A> iterator) {
+        List<A> r = new ArrayList<A>();
+        while (iterator.hasNext()) {
+            r.add(iterator.next());
+        }
+        return r;
+    }
+    
+    public static <A, R> List<R> mapToList(Iterable<A> source, Function<A, R> f) {
+        List<R> result;
+        if (source instanceof ArrayList<?>) {
+            // NOTE: optimization of most frequent operation
+            result = new ArrayList<R>(((ArrayList<?>) source).size());
+        } else {
+            result = new ArrayList<R>();
+        }
+        for (A item : source) {
+            result.add(f.apply(item));
+        }
+        return result;
+    }
+
+    public static <A, R> List<R> mapToListSimple(Iterable<A> source, Function<A, R> f) {
+        ArrayList<R> result = new ArrayList<R>();
+        for (A item : source) {
+            result.add(f.apply(item));
+        }
+        result.trimToSize();
+        return result;
+    }
+
+    public static <A> List<A> filterToList(Iterable<A> source, Function<A, Boolean> f) {
+        ArrayList<A> result;
+        if (source instanceof ArrayList<?>) {
+            result = new ArrayList<A>(((ArrayList<?>) source).size());
+        } else {
+            result = new ArrayList<A>();
+        }
+        for (A item : source) {
+            if (f.apply(item)) {
+                result.add(item);
+            }
+        }
+        result.trimToSize();
+        return result;
+    }
+}

src/java/org/jetbrains/jet/perf/iteratorList/lib/Function.java

+package org.jetbrains.jet.perf.iteratorList.lib;
+
+/**
+ * @author Stepan Koltsov
+ */
+public interface Function<A, R> {
+    R apply(A a);
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.