functional list benchmark and part of rewrite of type info codegen for performance reasons
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
abstract public class FList<T> {
|
||||
abstract T getHead();
|
||||
|
||||
abstract FList<T> getTail();
|
||||
|
||||
abstract FList<T> plus(T element);
|
||||
|
||||
static class Empty<T> extends FList<T> {
|
||||
|
||||
@Override
|
||||
T getHead() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
FList<T> getTail() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
FList<T> plus(T element) {
|
||||
return new OneElementList<T>(element);
|
||||
}
|
||||
}
|
||||
|
||||
static class OneElementList<T> extends FList<T> {
|
||||
private T element;
|
||||
|
||||
public OneElementList(T element) {
|
||||
super();
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
T getHead() {
|
||||
return element;
|
||||
}
|
||||
|
||||
@Override
|
||||
FList<T> getTail() {
|
||||
return new Empty<T>();
|
||||
}
|
||||
|
||||
@Override
|
||||
FList<T> plus(T element) {
|
||||
return new StandardList<T>(element, this);
|
||||
}
|
||||
}
|
||||
|
||||
private static class StandardList<T> extends FList<T> {
|
||||
private T head;
|
||||
private FList<T> tail;
|
||||
|
||||
public StandardList(T head, FList<T> tail) {
|
||||
this.head = head;
|
||||
this.tail = tail;
|
||||
}
|
||||
|
||||
@Override
|
||||
T getHead() {
|
||||
return head;
|
||||
}
|
||||
|
||||
@Override
|
||||
FList<T> getTail() {
|
||||
return tail;
|
||||
}
|
||||
|
||||
@Override
|
||||
FList<T> plus(T element) {
|
||||
return new StandardList<T>(element, this);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
for(int k = 0; k != 10; ++k) {
|
||||
long start = System.currentTimeMillis();
|
||||
FList<Integer> list = new Empty();
|
||||
for(int i = 0; i != 5000000; ++i)
|
||||
list = list.plus(i);
|
||||
System.out.println(System.currentTimeMillis()-start);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
namespace flist
|
||||
|
||||
abstract class FList<T> {
|
||||
abstract val head : T
|
||||
abstract val tail : FList<T>
|
||||
|
||||
abstract fun plus(element: T) : FList<T>
|
||||
}
|
||||
|
||||
class EmptyFList<T>() : FList<T> {
|
||||
override val head: T
|
||||
get() = throw java.util.NoSuchElementException()
|
||||
|
||||
override val tail : EmptyFList<T>
|
||||
get() = EmptyFList<T>()
|
||||
|
||||
override fun plus(element: T) : FList<T> = OneElementFList<T>(element)
|
||||
}
|
||||
|
||||
class OneElementFList<T> (override val head: T): FList<T> {
|
||||
override val tail : EmptyFList<T>
|
||||
get() = EmptyFList<T>()
|
||||
|
||||
override fun plus(element: T) : FList<T> = StandardFList<T>(element, this)
|
||||
}
|
||||
|
||||
class StandardFList<T> (override val head: T, override val tail: FList<T>) : FList<T> {
|
||||
override fun plus(element: T) : FList<T> = StandardFList<T>(element, this)
|
||||
}
|
||||
|
||||
fun <T> FList<T>.plus2(element: T): FList<T> =
|
||||
when(this) {
|
||||
is EmptyFList<T> => OneElementFList<T>(element)
|
||||
else => StandardFList<T>(element, this)
|
||||
}
|
||||
|
||||
fun <T> FList<T>.plus3(element: T) : FList<T> =
|
||||
if(this is EmptyFList<*>)
|
||||
OneElementFList<T>(element)
|
||||
else
|
||||
StandardFList<T>(element, this)
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
for(k in 0..3) {
|
||||
val start0 = System.currentTimeMillis()
|
||||
|
||||
var flist0 : FList<Int> = EmptyFList<Int>()
|
||||
for(i in 0..5000000)
|
||||
flist0 = flist0 + i
|
||||
|
||||
System.out?.println(System.currentTimeMillis() - start0)
|
||||
|
||||
val start = System.currentTimeMillis()
|
||||
|
||||
var flist : FList<Int> = EmptyFList<Int>()
|
||||
for(i in 0..5000000)
|
||||
flist = flist.plus2(i)
|
||||
|
||||
System.out?.println(System.currentTimeMillis() - start)
|
||||
|
||||
val start2 = System.currentTimeMillis()
|
||||
|
||||
var flist2 : FList<Int> = EmptyFList<Int>()
|
||||
for(i in 0..5000000)
|
||||
flist2 = flist2.plus3(i)
|
||||
|
||||
System.out?.println(System.currentTimeMillis() - start2)
|
||||
System.out?.println()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user