Created
October 6, 2017 12:40
-
-
Save taku0/fcd6af21faf9a835308512d2071cf2b4 to your computer and use it in GitHub Desktop.
Sample program for AOT for JDK 9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// javac AOTSample.java | |
// jaotc *.class | |
// time java AOTSample | |
// time java -XX:AOTLibrary=./unnamed.so AOTSample | |
public class AOTSample { | |
public static class Cons<T> { | |
private final T value; | |
private final Cons<T> next; | |
public Cons(T value, Cons<T> next) { | |
this.value = value; | |
this.next = next; | |
} | |
public T getValue() { | |
return value; | |
} | |
public Cons<T> getNext() { | |
return next; | |
} | |
} | |
public static class Queue<T> { | |
private final Cons<T> left; | |
private final Cons<T> right; | |
public Queue(Cons<T> left, Cons<T> right) { | |
this.left = left; | |
this.right = right; | |
} | |
public Queue<T> append(T value) { | |
return new Queue<T>(left, new Cons<T>(value, right)); | |
} | |
public Queue<T> tail() { | |
if (left == null) { | |
return new Queue<T>(reverse(right), null); | |
} else { | |
return new Queue<T>(left.getNext(), right); | |
} | |
} | |
private Cons<T> reverse(Cons<T> list) { | |
return doReverse(list, null); | |
} | |
private Cons<T> doReverse(Cons<T> list, Cons<T> result) { | |
if (list == null) { | |
return result; | |
} else { | |
return doReverse(list.getNext(), new Cons<T>(list.getValue(), result)); | |
} | |
} | |
public boolean isEmpty() { | |
return left == null && right == null; | |
} | |
private int size(Cons<T> list) { | |
if (list == null) { | |
return 0; | |
} else { | |
return size(list.getNext()) + 1; | |
} | |
} | |
public int size() { | |
return size(left) + size(right); | |
} | |
} | |
private static long[] s = new long[2]; | |
static { | |
s[0] = 100; | |
s[1] = 200; | |
} | |
private static long xorshift128Plus() { | |
long x = s[0]; | |
long y = s[1]; | |
s[0] = y; | |
x ^= x << 23; | |
s[1] = x ^ y ^ (x >>> 17) ^ (y >>> 26); | |
return s[1] + y; | |
} | |
public static void main(String... args) { | |
for (int j = 0; j < 100; j++) { | |
Queue<Integer> queue = new Queue<>(null, null); | |
for (int i = 0; i < 3000000; i++) { | |
if (queue.isEmpty() || xorshift128Plus() > 0) { | |
queue = queue.append(i); | |
} else { | |
queue = queue.tail(); | |
} | |
} | |
System.out.println(queue.size()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment