This repository was archived by the owner on Nov 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbench_test.go
295 lines (256 loc) · 6.34 KB
/
bench_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package golog
import "fmt"
import "strconv"
import "testing"
import "github.com/mndrix/golog/read"
import "github.com/mndrix/golog/term"
func BenchmarkTrue(b *testing.B) {
m := NewMachine()
g := read.Term_(`true.`)
for i := 0; i < b.N; i++ {
_ = m.ProveAll(g)
}
}
func BenchmarkAppend(b *testing.B) {
m := NewMachine().Consult(`
append([], A, A). % test same variable name as other clauses
append([A|B], C, [A|D]) :-
append(B, C, D).
`)
g := read.Term_(`append([a,b,c], [d,e], List).`)
for i := 0; i < b.N; i++ {
_ = m.ProveAll(g)
}
}
// unify two compounds terms with deep structure. unification succeeds
func BenchmarkUnifyDeep(b *testing.B) {
x := read.Term_(`a(b(c(d(e(f(g(h(i(j))))))))).`)
y := read.Term_(`a(b(c(d(e(f(g(h(i(X))))))))).`)
env := term.NewBindings()
for i := 0; i < b.N; i++ {
_, _ = x.Unify(env, y)
}
}
// unify two compounds terms with deep structure. unification fails
func BenchmarkUnifyDeepFail(b *testing.B) {
x := read.Term_(`a(b(c(d(e(f(g(h(i(j))))))))).`)
y := read.Term_(`a(b(c(d(e(f(g(h(i(x))))))))).`)
env := term.NewBindings()
for i := 0; i < b.N; i++ {
_, _ = x.Unify(env, y)
}
}
func BenchmarkUnificationHash(b *testing.B) {
x := read.Term_(`a(b(c(d(e(f(g(h(i(j))))))))).`)
for i := 0; i < b.N; i++ {
_ = term.UnificationHash([]term.Term{x}, 64, true)
}
}
// test performance of a standard maplist implementation
func BenchmarkMaplist(b *testing.B) {
m := NewMachine().Consult(`
always_a(_, a).
maplist(C, A, B) :-
maplist_(A, B, C).
maplist_([], [], _).
maplist_([B|D], [C|E], A) :-
call(A, B, C),
maplist_(D, E, A).
`)
g := read.Term_(`maplist(always_a, [1,2,3,4,5], As).`)
for i := 0; i < b.N; i++ {
_ = m.ProveAll(g)
}
}
// traditional, naive reverse benchmark
// The Art of Prolog by Sterling, etal says that reversing a 30 element
// list using this technique does 496 reductions. From this we can
// calculate a rough measure of Golog's LIPS.
func BenchmarkNaiveReverse(b *testing.B) {
m := NewMachine().Consult(`
append([], A, A).
append([A|B], C, [A|D]) :-
append(B, C, D).
reverse([],[]).
reverse([X|Xs], Zs) :-
reverse(Xs, Ys),
append(Ys, [X], Zs).
`)
g := read.Term_(`reverse([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], As).`)
for i := 0; i < b.N; i++ {
_ = m.ProveAll(g)
}
}
func BenchmarkDCGish(b *testing.B) {
m := NewMachine().Consult(`
name([alice |X], X).
name([bob |X], X).
name([charles |X], X).
name([david |X], X).
name([eric |X], X).
name([francis |X], X).
name([george |X], X).
name([harry |X], X).
name([ignatius|X], X).
name([john |X], X).
name([katie |X], X).
name([larry |X], X).
name([michael |X], X).
name([nancy |X], X).
name([oliver |X], X).
`)
g := read.Term_(`name([george,the,third], Rest).`)
for i := 0; i < b.N; i++ {
_ = m.ProveAll(g)
}
}
func BenchmarkRead(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = read.Term_(`reverse([1,2,3,4,5,6,7], Xs).`)
}
}
// Low level benchmarks to test Go's implementation
func init() { // avoid import errors when low level benchmarks comment out
_ = fmt.Sprintf("")
_ = strconv.Itoa(1)
}
/*
func BenchmarkLowLevelCompareUint64(b *testing.B) {
var nintendo uint64 = 282429536481
var other uint64 = 387429489
for i := 0; i < b.N; i++ {
if nintendo == other {
// do nothing
}
}
}
func BenchmarkLowLevelCompareString(b *testing.B) {
nintendo := "nintendo"
other := "other"
for i := 0; i < b.N; i++ {
if nintendo == other {
// do nothing
}
}
}
func BenchmarkLowLevelBitwise(b *testing.B) {
var nintendo uint64 = 282429536481
var other uint64 = 387429489
for i := 0; i < b.N; i++ {
if nintendo&other == nintendo {
// do nothing
}
}
}
func BenchmarkLowLevelFloatBinaryExponent(b *testing.B) {
f := 3.1415
for i := 0; i < b.N; i++ {
_ = strconv.FormatFloat(f, 'b', 0, 64)
}
}
func BenchmarkLowLevelFloatDecimalExponent(b *testing.B) {
f := 3.1415
for i := 0; i < b.N; i++ {
_ = strconv.FormatFloat(f, 'e', 64, 64)
}
}
func BenchmarkLowLevelIntDecimal(b *testing.B) {
var x uint64 = 1967
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%d", x)
}
}
func BenchmarkLowLevelIntHex(b *testing.B) {
var x uint64 = 1967
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%x", x)
}
}
// benchmarks to compare performance on interface-related code
type AnInterface interface {
AMethod() int
}
type ImplementationOne int
func (*ImplementationOne) AMethod() int { return 1 }
type ImplementationTwo int
func (*ImplementationTwo) AMethod() int { return 2 }
func NotAMethod(x AnInterface) int {
switch x.(type) {
case *ImplementationOne:
return 1
case *ImplementationTwo:
return 2
}
panic("impossible")
}
func NotAMethodManual(x AnInterface) int {
kind := x.AMethod()
switch kind {
case 1:
return 1
case 2:
return 2
}
panic("impossible")
}
// how expensive is it to call a method?
func BenchmarkInterfaceMethod(b *testing.B) {
var x AnInterface
num := 100
x = (*ImplementationOne)(&num)
for i := 0; i < b.N; i++ {
_ = x.AMethod()
}
}
// how expensive is it to call a function that acts like a method?
func BenchmarkInterfaceFunctionTypeSwitch(b *testing.B) {
var x AnInterface
num := 100
x = (*ImplementationOne)(&num)
for i := 0; i < b.N; i++ {
_ = NotAMethod(x)
}
}
// how expensive is it to call a function that acts like a method?
func BenchmarkInterfaceFunctionManualTypeSwitch(b *testing.B) {
var x AnInterface
num := 100
x = (*ImplementationOne)(&num)
for i := 0; i < b.N; i++ {
_ = NotAMethodManual(x)
}
}
// how expensive is it to inline a type switch that acts like a method?
func BenchmarkInterfaceInlineTypeSwitch(b *testing.B) {
var x AnInterface
num := 100
x = (*ImplementationOne)(&num)
for i := 0; i < b.N; i++ {
var y int
switch x.(type) {
case *ImplementationOne:
y = 1
case *ImplementationTwo:
y = 2
}
_ = y
}
}
// how expensive is a manually-implemented type switch?
func BenchmarkInterfaceManualTypeSwitch(b *testing.B) {
var x AnInterface
num := 100
x = (*ImplementationOne)(&num)
for i := 0; i < b.N; i++ {
var y int
kind := x.AMethod()
switch kind {
case 1:
y = 1
case 2:
y = 2
}
_ = y
}
}
*/