/** * Created with IntelliJ IDEA. * User: Oliver Plohmann * Date: 25.08.12 * Time: 15:31 * To change this template use File | Settings | File Templates. */ open class Fib() { class object { fun fibStaticIf(n: Int): Int { if(n >= 2) return fibStaticIf(n - 1) + fibStaticIf(n - 2) else return 1 } fun fibStaticTernaryIf(n: Int): Int { return if(n >= 2) fibStaticTernaryIf(n - 1) + fibStaticTernaryIf(n - 2) else 1 } } fun fibIf(n: Int): Int { if (n >= 2) return fibIf(n - 1) + fibIf(n - 2) else return 1 } fun fibTernaryIf(n: Int): Int { return if (n >= 2) fibTernaryIf(n - 1) + fibTernaryIf(n - 2) else 1 } } fun main(args : Array) { var warmup = true var warmUpIters = 50 if (warmup) { for (i in 1 .. warmUpIters) { Fib.fibStaticTernaryIf(40) print(".") } } var start = System.currentTimeMillis(); Fib.fibStaticTernaryIf(40); System.out.println("Kotlin(static ternary): " + (System.currentTimeMillis() - start) + "ms") if (warmup) { for ( i in 1 .. warmUpIters) { Fib.fibStaticIf(40) print(".") } } start = System.currentTimeMillis() Fib.fibStaticIf(40) System.out.println("Kotlin(static if): " + (System.currentTimeMillis() - start) + "ms") var fib = Fib() if (warmup) { for ( i in 1 .. warmUpIters) { fib.fibTernaryIf(40); print(".") } } start = System.currentTimeMillis(); fib.fibTernaryIf(40); System.out.println("Kotlin(instance ternary): " + (System.currentTimeMillis() - start) + "ms"); if (warmup) { for ( i in 1 .. warmUpIters) { fib.fibIf(40); print(".") } } start = System.currentTimeMillis(); fib.fibIf(40); System.out.println("Kotlin(instance if): " + (System.currentTimeMillis() - start) + "ms"); }