-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_test.go
130 lines (122 loc) · 2.65 KB
/
event_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
// molecular is a 3D physics engine written in Go
// Copyright (C) 2023 Kevin Z <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package molecular_test
import (
. "github.com/LiterMC/molecular"
"math"
"testing"
)
const (
maxR0 = (float64)(int(1)<<(iota*2)) * (C / 100.)
maxR1
maxR2
maxR3
maxR4
maxR5
maxR6
maxR7
maxR8
)
type eventWaveFn struct {
radius float64
delay int
}
func beforeTickSwitch(e *eventWaveFn) bool {
switch {
case e.radius > maxR8:
if e.delay != 1<<(8*2) {
e.delay = 1 << (8 * 2)
}
case e.radius > maxR7:
if e.delay != 1<<(7*2) {
e.delay = 1 << (7 * 2)
}
case e.radius > maxR6:
if e.delay != 1<<(6*2) {
e.delay = 1 << (6 * 2)
}
case e.radius > maxR5:
if e.delay != 1<<(5*2) {
e.delay = 1 << (5 * 2)
}
case e.radius > maxR4:
if e.delay != 1<<(4*2) {
e.delay = 1 << (4 * 2)
}
case e.radius > maxR3:
if e.delay != 1<<(3*2) {
e.delay = 1 << (3 * 2)
}
case e.radius > maxR2:
if e.delay != 1<<(2*2) {
e.delay = 1 << (2 * 2)
}
case e.radius > maxR1:
if e.delay != 1<<(1*2) {
e.delay = 1 << (1 * 2)
}
case e.radius > maxR0:
if e.delay != 1<<(0*2) {
e.delay = 1 << (0 * 2)
}
}
return false
}
func beforeTickMath(e *eventWaveFn) bool {
if e.radius >= maxR8 {
if e.delay != 1<<(8*2) {
e.delay = 1 << (8 * 2)
}
} else {
n := (int)(math.Log2(e.radius / (C / 100.)))
if n > 0 {
if e.delay != 1<<n {
e.delay = 1 << n
}
}
}
return false
}
func BenchmarkEventWaveRadiusCheckSwitch(b *testing.B) {
events := make([]*eventWaveFn, 10)
for i, _ := range events {
events[i] = &eventWaveFn{
radius: r.Float64() * C * 2,
delay: 0,
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, e := range events {
beforeTickSwitch(e)
}
}
}
func BenchmarkEventWaveRadiusCheckMath(b *testing.B) {
events := make([]*eventWaveFn, 10)
for i, _ := range events {
events[i] = &eventWaveFn{
radius: r.Float64() * C * 2,
delay: 0,
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, e := range events {
beforeTickMath(e)
}
}
}