github.com/hraberg/cljs2go@v0.0.0-20160502191955-9d268a4feb92/bench_test.go (about) 1 package main 2 3 import "testing" 4 5 import "github.com/stretchr/testify/assert" 6 7 import . "github.com/hraberg/cljs2go/cljs/core" 8 9 func Benchmark_RecursiveDirectCall(t *testing.B) { 10 fib := func(this *AFn) *AFn { 11 return Fn(this, func(n interface{}) interface{} { 12 if n == 0.0 { 13 return 0.0 14 } else if n == 1.0 { 15 return 1.0 16 } else { 17 return this.X_invoke_Arity1(n.(float64)-1.0).(float64) + 18 this.X_invoke_Arity1(n.(float64)-2.0).(float64) 19 } 20 }) 21 }(&AFn{}) 22 assert.Equal(t, 832040, fib.X_invoke_Arity1(30.0)) 23 } 24 25 func Benchmark_RecursiveDirectPrimitiveCall(t *testing.B) { 26 fib := func(this *AFn) *AFn { 27 return Fn(this, func(n float64) float64 { 28 if n == 0.0 { 29 return 0.0 30 } else if n == 1.0 { 31 return 1.0 32 } else { 33 return this.Arity1FF(n-1.0) + this.Arity1FF(n-2.0) 34 } 35 }) 36 }(&AFn{}) 37 assert.Equal(t, 832040, fib.X_invoke_Arity1(30.0)) 38 } 39 40 func Benchmark_RecursiveDispatch(t *testing.B) { 41 fib := func(this *AFn) *AFn { 42 return Fn(this, func(a interface{}) interface{} { 43 var n = a.(float64) 44 if n == 0.0 { 45 return 0.0 46 } else if n == 1.0 { 47 return 1.0 48 } else { 49 return this.Call(n-1.0).(float64) + this.Call(n-2.0).(float64) 50 } 51 }) 52 }(&AFn{}) 53 assert.Equal(t, 832040, fib.Call(30.0)) 54 } 55 56 func Benchmark_RecursiveGo(t *testing.B) { 57 fib := func() func(float64) float64 { 58 var this func(float64) float64 59 this = func(n float64) float64 { 60 if n == 0 { 61 return 0 62 } else if n == 1 { 63 return 1 64 } else { 65 return this(n-1) + this(n-2) 66 } 67 } 68 return this 69 }() 70 assert.Equal(t, 832040, fib(30)) 71 }