This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
benchmark_test.go
86 lines (72 loc) · 1.52 KB
/
benchmark_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
package ssainterp_test
import (
"testing"
"go/types"
"github.com/go-interpreter/ssainterp"
"github.com/go-interpreter/ssainterp/interp"
orig "golang.org/x/tools/go/ssa/interp"
)
// just using http://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go
func Fib(n int) int {
if n < 2 {
return n
}
return Fib(n-1) + Fib(n-2)
}
func BenchmarkFib10Native(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
Fib(10)
}
}
const codeFib = `
package main
func main(){}
func Fib(n int) int {
if n < 2 {
return n
}
return Fib(n-1) + Fib(n-2)
}
`
var ctx *ssainterp.Interpreter
func init() {
var err error
ctx, _, err = ssainterp.Run(codeFib, nil, nil, nil)
if err != nil {
panic(err)
}
}
func BenchmarkFib10SSAinterp(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
ctx.Call("main.Fib", []interp.Ivalue{interp.Ivalue(10)})
}
}
const codeFib10Orig = `
package main
func main(){ Fib(10) }
func Fib(n int) int {
if n < 2 {
return n
}
return Fib(n-1) + Fib(n-2)
}
`
var ctxOrig *ssainterp.Interpreter
func init() {
var err error
ctxOrig, _, err = ssainterp.Run(codeFib10Orig, nil, nil, nil)
if err != nil {
panic(err)
}
}
func BenchmarkFib10Orig(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
ec := orig.Interpret(ctxOrig.MainPkg, 0, &types.StdSizes{8, 8}, "filename.go", nil)
if ec != 0 {
b.Fatalf("returned error code non-zero:%d", ec)
}
}
}