Commits

Stepan Koltsov  committed 1c390a6

backup

  • Participants
  • Parent commits a3e7aea

Comments (0)

Files changed (10)

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

 
 import org.jetbrains.jet.perf.iteratorList.lib.Function;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
         return r;
     }
 
-    public static int hash(int i, int seed) {
-        return i * seed * 0x9e3779b1;
+    public static int hash(int a, int b, int c) {
+        return hash(hash(a, b), c);
+    }
+
+    public static int hash(int a, int b) {
+        return hash(a) ^ hash(b);
+    }
+
+    public static int hash(int i) {
+        return i * 0x9e3779b1;
+    }
+
+    public static int randomInRange(int max, int seed) {
+        return (hash(seed, max) & 0x7FFFFFFF) % max;
     }
 
     public static Function<Integer, Integer> plus1F() {
             }
         };
     }
+
+    public static void write(File file, CharSequence string) throws IOException {
+        FileOutputStream os = new FileOutputStream(file);
+        try {
+                os.write(string.toString().getBytes("utf-8"));
+                os.close();
+            } finally {
+                os.close();
+            }
+    }
 }

File src/java/org/jetbrains/jet/perf/invokevirtual/InvokeVirtualTest.java

+package org.jetbrains.jet.perf.invokevirtual;
+
+import org.jetbrains.jet.perf.TestUtils;
+
+import java.util.*;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class InvokeVirtualTest {
+
+    public static void main(String[] args) {
+        for (int i = 0; i < 3; ++i) {
+            new InvokeVirtualTest(i).iteration(false);
+        }
+        for (int i = 0; ; ++i) {
+            new InvokeVirtualTest(i).iteration(true);
+        }
+    }
+
+    private final int iteration;
+
+    public InvokeVirtualTest(int iteration) {
+        this.iteration = iteration;
+    }
+
+    private Random random = new Random();
+
+    private static EnumMap<Kind, Long> mins = new EnumMap<Kind, Long>(Kind.class);
+
+    static {
+        for (Kind what : Kind.values()) {
+            mins.put(what, Long.MAX_VALUE);
+        }
+    }
+
+    private EnumMap<Kind, Integer> counts = new EnumMap<Kind, Integer>(Kind.class);
+
+    enum Kind {
+        EMPTY_LIST,
+        ARRAY_LIST,
+        ARL_CACHED,
+    }
+
+    private int minCount() {
+        int minCount = Integer.MAX_VALUE;
+        for (Kind kind : Kind.values()) {
+            int count = counts.get(kind);
+            if (count < minCount) {
+                minCount = count;
+            }
+        }
+        return minCount;
+    }
+
+    private void iteration(boolean record) {
+        for (Kind what : Kind.values()) {
+            counts.put(what, 0);
+        }
+
+        System.out.println("iteration " + iteration + (record ? "" : " not recording"));
+        int seed = random.nextInt();
+        while (minCount() < 3) {
+            Kind what = Kind.values()[random.nextInt(3)];
+            long start = System.currentTimeMillis();
+            int sum;
+            switch (what) {
+                case EMPTY_LIST:
+                    sum = runEmptyList(seed);
+                    break;
+                case ARRAY_LIST:
+                    sum = runArrayList(seed);
+                    break;
+                case ARL_CACHED:
+                    sum = runArrayListCached(seed);
+                    break;
+                default:
+                    throw new AssertionError();
+            }
+            long d = System.currentTimeMillis() - start;
+            System.out.println(what.toString().toLowerCase() + ": " + d + "ms, sum=" + sum + ", mc=" + minCount());
+
+            if (record) {
+                if (mins.get(what) > d) {
+                    mins.put(what, d);
+                }
+            }
+
+            counts.put(what, counts.get(what) + 1);
+        }
+
+        if (record) {
+            System.out.println("mins:");
+            for (Kind what : Kind.values()) {
+                System.out.println(what.toString().toLowerCase() + ": " + mins.get(what) + "ms");
+            }
+        }
+    }
+
+    private static final int count = 50000000;
+
+    private int runArrayList(int seed) {
+        int sum = 0;
+        for (int i = 0; i < count; ++i) {
+            List<Integer> list = intsArrayList(TestUtils.hash(i, seed));
+            for (Integer item : list) {
+                sum += item;
+            }
+        }
+        return sum;
+    }
+
+    private int runArrayListCached(int seed) {
+        int sum = 0;
+        for (int i = 0; i < count; ++i) {
+            List<Integer> list = intsArrayListCached(TestUtils.hash(i, seed));
+            for (Integer item : list) {
+                sum += item;
+            }
+        }
+        return sum;
+    }
+
+    private int runEmptyList(int seed) {
+        int sum = 0;
+        for (int i = 0; i < count; ++i) {
+            List<Integer> list = intsEmptyList(TestUtils.hash(i, seed));
+            for (Integer item : list) {
+                sum += item;
+            }
+        }
+        return sum;
+    }
+
+
+
+
+    private static final Integer[] ints = {
+            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+    private Integer randomInteger(int seed) {
+        return ints[TestUtils.randomInRange(ints.length, seed)];
+    }
+
+    private List<Integer> intsArrayList(int seed) {
+        int length = TestUtils.randomInRange(3, seed);
+        if (length == 0) {
+            return new ArrayList<Integer>(0);
+        }
+        List<Integer> list = new ArrayList<Integer>(length);
+        for (int i = 0; i < length; ++i) {
+            list.add(randomInteger(TestUtils.hash(i, seed)));
+        }
+        return list;
+    }
+
+    private List<Integer> intsEmptyList(int seed) {
+        int length = TestUtils.randomInRange(3, seed);
+        if (length == 0) {
+            return Collections.emptyList();
+        }
+        List<Integer> list = new ArrayList<Integer>(length);
+        for (int i = 0; i < length; ++i) {
+            list.add(randomInteger(TestUtils.hash(i, seed)));
+        }
+        return list;
+    }
+
+    private static final ArrayList<?> emptyArrayList = new ArrayList<Object>(0);
+
+    private List<Integer> intsArrayListCached(int seed) {
+        int length = TestUtils.randomInRange(3, seed);
+        if (length == 0) {
+            return (List<Integer>) emptyArrayList;
+        }
+        List<Integer> list = new ArrayList<Integer>(length);
+        for (int i = 0; i < length; ++i) {
+            list.add(randomInteger(TestUtils.hash(i, seed)));
+        }
+        return list;
+    }
+
+}

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

 package org.jetbrains.jet.perf.iteratorList;
 
+import org.jetbrains.jet.perf.TestUtils;
 import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
-import org.jetbrains.jet.perf.util.TestUtils;
 
 import java.util.List;
 

File src/java/org/jetbrains/jet/perf/iteratorList/IteratorListTestUtils.java

 package org.jetbrains.jet.perf.iteratorList;
 
+import org.jetbrains.jet.perf.TestUtils;
 import org.jetbrains.jet.perf.iteratorList.lib.Function;
-import org.jetbrains.jet.perf.util.TestUtils;
 
 import java.util.ArrayList;
 import java.util.List;

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

 package org.jetbrains.jet.perf.iteratorList;
 
+import org.jetbrains.jet.perf.TestUtils;
 import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
-import org.jetbrains.jet.perf.util.TestUtils;
 
 import java.util.List;
 

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

 package org.jetbrains.jet.perf.iteratorList;
 
+import org.jetbrains.jet.perf.TestUtils;
 import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
-import org.jetbrains.jet.perf.util.TestUtils;
 
 import java.util.List;
 

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

 package org.jetbrains.jet.perf.iteratorList;
 
+import org.jetbrains.jet.perf.TestUtils;
 import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
-import org.jetbrains.jet.perf.util.TestUtils;
 
 import java.util.List;
 

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

 package org.jetbrains.jet.perf.iteratorList;
 
+import org.jetbrains.jet.perf.TestUtils;
 import org.jetbrains.jet.perf.iteratorList.lib.CollectionUtils;
-import org.jetbrains.jet.perf.util.TestUtils;
 
 import java.io.File;
 import java.util.HashMap;

File src/java/org/jetbrains/jet/perf/reified/ReifiedVsErased.java

 package org.jetbrains.jet.perf.reified;
 
-import org.jetbrains.jet.perf.util.TestUtils;
+import org.jetbrains.jet.perf.TestUtils;
 
 import java.io.File;
 import java.util.*;

File src/java/org/jetbrains/jet/perf/util/TestUtils.java

-package org.jetbrains.jet.perf.util;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-/**
- * @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 int hash(int i, int seed) {
-        return i * seed * 0x9e3779b1;
-    }
-
-    public static void write(File file, CharSequence string) throws IOException {
-        FileOutputStream os = new FileOutputStream(file);
-        try {
-            os.write(string.toString().getBytes("utf-8"));
-            os.close();
-        } finally {
-            os.close();
-        }
-    }
-}