forked from etnz/boolgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilders_test.go
44 lines (39 loc) · 1.06 KB
/
builders_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
package boolgebra
import (
"strconv"
"testing"
)
// BenchmarkAnd using And as a accumulator function is counter productive.
// like in for `{ res = And(res, x[i]) }`
//
// This benchmark is to show that, in comparision with BenchmarkTermBuilder
func BenchmarkAnd(b *testing.B) {
res := Lit(true) // neutral for And
// prepare 1000 different ids
M := 1000
var ids []string
for i := 0; i < M; i++ {
ids = append(ids, "parameter_"+strconv.Itoa(i))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
res = And(res, ID(ids[i%M]))
}
}
// BenchmarkTermBuilder does the same as BenchmarkAnd but using the TermBuilder.
//
// TermBuilder is much more faster than using And over and over. Indeed, the API uses
// immutable object that require a lot of memory allocation. The builder save those allocations
func BenchmarkTermBuilder(b *testing.B) {
// prepare 1000 different ids
M := 1000
var ids []string
for i := 0; i < M; i++ {
ids = append(ids, "parameter_"+strconv.Itoa(i))
}
var t TermBuilder
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.And(ids[i%M], true)
}
}