-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclock.go
More file actions
87 lines (70 loc) · 1.57 KB
/
Copy pathclock.go
File metadata and controls
87 lines (70 loc) · 1.57 KB
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
package minesweeper
import (
"time"
)
var globalTimer time.Duration
const Years150 = time.Hour * 24 * 365 * 150
func UpdateGlobalTimer() {
globalTimer += UpdateDelta()
}
func GlobalTimerNow() time.Duration {
return globalTimer
}
func TimeSinceNow(t time.Duration) time.Duration {
return GlobalTimerNow() - t
}
type Timer struct {
Duration time.Duration
Current time.Duration
}
func (t *Timer) TickUp() {
t.Current += UpdateDelta()
}
func (t *Timer) TickDown() {
t.Current -= UpdateDelta()
}
func (t *Timer) ClampCurrent() {
t.Current = Clamp(t.Current, 0, t.Duration)
}
func (t *Timer) Normalize() float64 {
if t.Duration == 0 {
return 0
}
return Clamp(f64(t.Current)/f64(t.Duration), 0, 1)
}
func (t *Timer) NormalizeUnclamped() float64 {
if t.Duration == 0 {
return 0
}
return f64(t.Current) / f64(t.Duration)
}
func (t *Timer) InRange(rangeStart, rangeEnd time.Duration) bool {
return rangeStart <= t.Current && t.Current <= rangeEnd
}
func (t *Timer) NormalizeRange(rangeStart, rangeEnd time.Duration) float64 {
normalized := f64(t.Current-rangeStart) / f64(rangeEnd-rangeStart)
normalized = Clamp(normalized, 0, 1)
return normalized
}
// Timer for profiling.
// Usage :
//
// {
// timer := NewProfTimer("some function")
// defer timer.Report()
// // reports some function took 10ms
// }
type ProfTimer struct {
Start time.Time
Name string
}
func NewProfTimer(name string) ProfTimer {
return ProfTimer{
Start: time.Now(),
Name: name,
}
}
func (p ProfTimer) Report() {
now := time.Now()
InfoLogger.Printf("\"%v\" took %v\n", p.Name, now.Sub(p.Start))
}