...
1
2
3
4
5
6
7
8 package mult
9
10 var sink int
11
12 type Multiplier interface {
13 Multiply(a, b int) int
14 }
15
16 type Mult struct{}
17
18 func (Mult) Multiply(a, b int) int {
19 for i := 0; i < 1000; i++ {
20 sink++
21 }
22 return a * b
23 }
24
25 type NegMult struct{}
26
27 func (NegMult) Multiply(a, b int) int {
28 for i := 0; i < 1000; i++ {
29 sink++
30 }
31 return -1 * a * b
32 }
33
34
35 type MultFunc func(int64, int64) int64
36
37 func MultFn(a, b int64) int64 {
38 for i := 0; i < 1000; i++ {
39 sink++
40 }
41 return a * b
42 }
43
44 func NegMultFn(a, b int64) int64 {
45 for i := 0; i < 1000; i++ {
46 sink++
47 }
48 return -1 * a * b
49 }
50
51
52 func MultClosure() MultFunc {
53
54 c := 1
55 return func(a, b int64) int64 {
56 for i := 0; i < 1000; i++ {
57 sink++
58 }
59 return a * b * int64(c)
60 }
61 }
62
63
64 func NegMultClosure() MultFunc {
65 c := 1
66 return func(a, b int64) int64 {
67 for i := 0; i < 1000; i++ {
68 sink++
69 }
70 return -1 * a * b * int64(c)
71 }
72 }
73
View as plain text