-
Notifications
You must be signed in to change notification settings - Fork 10
/
benchmarks_test.go
112 lines (90 loc) · 1.92 KB
/
benchmarks_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package binder
import (
"errors"
"fmt"
"testing"
)
const (
testFibonacciIterations = 30
)
func TestBenchmarks_Assert(t *testing.T) {
bndr := New()
bndr.Func("fib_go", func(c *Context) error {
t := c.Top()
if t != 1 {
return errors.New("need an argument")
}
c.Push().Number(fib(c.Arg(1).Number()))
return nil
})
if err := bndr.DoString(`
function fib_lua(n)
if n<3 then
return 1
else
return fib_lua(n-1) + fib_lua(n-2)
end
end
assert(fib_go(3) == fib_lua(3), 'fib_go(3) != fib_lua(3)')
assert(fib_go(10) == fib_lua(10), 'fib_go(10) != fib_lua(10)')
assert(fib_go(30) == fib_lua(30), 'fib_go(30) != fib_lua(30)')
`); err != nil {
t.Error("Lua and go funcs are not equal", err)
}
}
func BenchmarkFibonacci_Go(b *testing.B) {
benchNative(New(Options{
SkipOpenLibs: true,
}), b)
}
func BenchmarkFibonacci_Go_OpenLibs(b *testing.B) {
benchNative(New(), b)
}
func BenchmarkFibonacci_Lua(b *testing.B) {
benchLua(New(Options{
SkipOpenLibs: true,
}), b)
}
func BenchmarkFibonacci_Lua_OpenLibs(b *testing.B) {
benchLua(New(), b)
}
// fib is go native version of lua fib()
func fib(n float64) float64 {
if n < 3 {
return 1
}
return fib(n-1) + fib(n-2)
}
func benchNative(bndr *Binder, b *testing.B) {
bndr.Func("fib", func(c *Context) error {
t := c.Top()
if t != 1 {
return errors.New("need an argument")
}
c.Push().Number(fib(c.Arg(1).Number()))
return nil
})
s := `
for i = 1, %d do
fib(%d)
end`
if err := bndr.DoString(fmt.Sprintf(s, b.N, testFibonacciIterations)); err != nil {
b.Error("Error Fibonacci go calculation", err)
}
}
func benchLua(bndr *Binder, b *testing.B) {
s := `
function fib(n)
if n<3 then
return 1
else
return fib(n-1) + fib(n-2)
end
end
for i = 1, %d do
fib(%d)
end`
if err := bndr.DoString(fmt.Sprintf(s, b.N, testFibonacciIterations)); err != nil {
b.Error("Error Fibonacci go calculation", err)
}
}