generated from roman-mazur/architecture-lab-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
67 lines (62 loc) · 1.13 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
package lab2
import (
"fmt"
"testing"
)
var operators = []string{
"+",
"-",
"*",
"/",
}
type BenchmarkCase struct {
operands []int
expression string
}
func BuildBenchmarkCase(count int) *BenchmarkCase {
operands := []int{}
expression := ""
n := count * count * count * count
for i := 1; i <= n; i++ {
operands = append(operands, i)
}
i := 0
j := 0
step := 2
for i < len(operands) {
expression += fmt.Sprint(operands[i])
if i == len(operands)-1 {
expression += " " + operators[j]
} else if i != 0 && i%(step-1) == 0 {
expression += " " + operators[j]
j++
if j == len(operators) {
j = 0
}
step += 2
if i%5 == 0 {
step = 2
}
}
if i != len(operands) {
expression += " "
}
i++
}
return &BenchmarkCase{
operands: operands,
expression: expression,
}
}
func BenchmarkCalculatePostfix(b *testing.B) {
cases := []*BenchmarkCase{}
for i := 2; i <= 20; i++ {
cases = append(cases, BuildBenchmarkCase(i))
}
for _, entry := range cases {
title := fmt.Sprintf("operands=%d", len(entry.operands))
b.Run(title, func(b *testing.B) {
CalculatePostfix(entry.expression)
})
}
}